WPF组合框与ItemsSource - 我设置哪个路径访问这个数据绑定? [英] WPF Combobox with ItemsSource - What path do I set to access this databinding?

查看:165
本文介绍了WPF组合框与ItemsSource - 我设置哪个路径访问这个数据绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XAML中设置了Binding / Path属性时遇到问题。



我知道这个ComboBox的 ItemSource 属性正确更新,因为当我更新 viewmodel (而不是文本框,文本,这是我期望的),因为我收到一堆空的文本框。 / p>

所以我相信我的ComboBox的 DataTemplate 部分中的绑定需要一个不同的绑定路径,但我不是确定要将绑定路径设置为。

 < ComboBox ItemsSource ={Binding Path = Locations}Visibility ={绑定SettingsOptionsVisibility}Grid.Column =0x:Name =locationCBVerticalAlignment =TopSelectionChanged =locationCB_SelectionChangedHorizo​​ntalAlignment =LeftWidth =350Height =30IsHitTestVisible =FalseIsEnabled = FalseFocusable =False> 
< ComboBox.ItemTemplate>
< DataTemplate>
< TextBlock Text ={Binding Name}>< / TextBlock>
< / DataTemplate>
< /ComboBox.ItemTemplate>
< / ComboBox>

以下是我的Codebehind中WatchCB的ItemsSource的照片: http://imgur.com/x4SYWER



如预期的那样,我的组合框中填有8个(无文本)元素。我需要做什么才能将文本绑定到名称以连接?



编辑:ViewModel中的Locations对象的代码:

  public ObservableCollection< Location>地点{get;组; } 

根据请求,包含在observable集合中的Location类的代码:

  public class Location 
{
public Guid LocationID;
public Guid ParentID;
public String Name;
public bool isValid;
}


解决方案

更改位置的字段对象属性:

  public class Location 
{
public Guid LocationID {get;组; }
public Guid ParentID {get;组; }
public String Name {get;组; }
public bool isValid {get;组; }
}


I'm having a problem setting the Binding/Path property in my XAML.

I know this ComboBox's ItemSource property is updating properly, since I get a bunch of empty text-boxes when I update the viewmodel (instead of textboxes with text, which is what I expect).

So I believe the Binding in the DataTemplate section of my ComboBox needs a different binding path, but I'm not sure what to set the binding path to.

<ComboBox ItemsSource="{Binding Path=Locations}" Visibility="{Binding SettingsOptionsVisibility}" Grid.Column="0" x:Name="locationCB" VerticalAlignment="Top" SelectionChanged="locationCB_SelectionChanged"  HorizontalAlignment="Left" Width="350" Height="30" IsHitTestVisible="False" IsEnabled="False" Focusable="False">
    <ComboBox.ItemTemplate>
        <DataTemplate>
                <TextBlock  Text="{Binding Name}"></TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Here's a picture of my LocationCB's ItemsSource in the Watch List in my Codebehind: http://imgur.com/x4SYWER

As expected, my combobox is populated with 8 (textless) elements. What do I need to do to get my binding the text to Name to connect up?

EDIT: code for the Locations object in the ViewModel:

public ObservableCollection<Location> Locations { get; set; }

And code for the Location class wrapped in the observable collection, as requested:

public class Location
{
    public Guid LocationID;
    public Guid ParentID;
    public String Name;
    public bool isValid;
}

解决方案

Change the fields of your location object to properties:

public class Location
{
    public Guid LocationID { get; set; }
    public Guid ParentID { get; set; }
    public String Name { get; set; }
    public bool isValid { get; set; }
}

这篇关于WPF组合框与ItemsSource - 我设置哪个路径访问这个数据绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆