WPF Combobox与ItemsSource - 我设置访问此数据绑定的路径? [英] WPF Combobox with ItemsSource - What path do I set to access this databinding?

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

问题描述

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



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

所以我相信在我的ComboBox的 DataTemplate 部分的Binding需要一个不同的绑定路径,

 < ComboBox ItemsSource ={Binding Path = Locations}Visibility ={ Binding 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>

这是我的CodeCB的ItemsSource在我的Codebehind中的监视列表的图片: 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 Combobox与ItemsSource - 我设置访问此数据绑定的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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