简体   繁体   English

WPF:comboBox数据绑定问题

[英]WPF: comboBox data binding issue

I have a public class LPosition , which contains an attribute public String OZ { get; set; } 我有一个public class LPosition ,它包含一个属性public String OZ { get; set; } public String OZ { get; set; } public String OZ { get; set; } . public String OZ { get; set; } The application reads .txt and .xml files and OZ gets the values from these files. 应用程序读取.txt和.xml文件,OZ从这些文件中获取值。 I need to bind OZ to comboBox: 我需要将OZ绑定到comboBox:

<ComboBox x:Name="OZs" SelectionChanged="OZs_SelectionChanged" Margin="-10,0,10,1" Grid.Column="0" Grid.Row="1" Height="27" VerticalAlignment="Bottom">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding OZ}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

As you see, I have tried to implement data binding using DataTemplate, but it doesn't work. 如您所见,我试图使用DataTemplate实现数据绑定,但是它不起作用。 I know that it's possible to implement data binding with ItemsSource, but for this I need an ObservableCollection, which I don't have and I don't know how to create it from OZ. 我知道可以用ItemsSource实现数据绑定,但是为此,我需要一个ObservableCollection,我没有它,也不知道如何从OZ创建它。 I have seen many examples where an ObservableCollection is hard coded, and I understand how to use it when it's hard coded, but I have no idea what to do in my case. 我已经看到了很多示例,其中ObservableCollection是硬编码的,并且我了解在对其进行硬编码时如何使用它,但是我不知道该怎么做。
Sorry if the explanation is not clear, I'm very new to WPF. 抱歉,如果解释不清楚,我是WPF的新手。 And I'm very lost with this problem. 我对这个问题很迷失。 Any help would be appreciated. 任何帮助,将不胜感激。

edit: according to the answer of @Xiaoy312 I've added following code: 编辑:根据@ Xiaoy312的答案,我添加了以下代码:

public IEnumerable<LPosition> OZList { get; set; } 

public FormelAssistent()
{
    InitializeComponent();
    this.DataContext = this;
    OZs.ItemsSource = OZList;
}

and XAML: 和XAML:

<ComboBox x:Name="OZs"
          SelectionChanged="OZs_SelectionChanged"
          Margin="-10,0,10,1"
          Grid.Column="0"
          Grid.Row="1"
          Height="27"
          VerticalAlignment="Bottom"
          ItemsSource="{Binding OZList}"
          DisplayMemberPath="OZ" />

But it doesn't work :( 但这不起作用:(

You need to provide a list of contents(options) for the ComboBox to choose from via its ItemsSource property. 您需要提供一个内容列表(选项),以便ComboBox通过其ItemsSource属性进行选择。 You don't necessarily need to back it with a ObservableCollection . 您不一定需要使用ObservableCollection支持它。 Only if you plan to add/remove your collection while your view is being presented. 仅当您计划在显示视图时添加/删除集合时。 Any IEnumerable could work too. 任何IEnumerable都可以工作。

<ComboBox x:Name="OZs"
          ItemsSource="{Binding ListOrIEnumerableOfLPosition}"
          DisplayMemberPath="OZ" <- you can also use this instead of setting an item template, unless you need something complex
          ...>
    ...
</ComboBox >

If you implement the INotifyPropertyChanged interface on your class like so, it should work. 如果像这样在类上实现INotifyPropertyChanged接口,它应该可以工作。

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string PropertyName = "")
{
    if(PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
    }
}

private IEnumerable<string> _oz;
public IEnumerable<string> Oz
{
    get
    {
        return _oz;
    }
    set
    {
        _oz = value;
        NotifyPropertyChanged("Oz");
    }
}

If you happen to be using a dependency object, you can instead use a dependency property. 如果碰巧正在使用依赖项对象,则可以改用依赖项属性。 This would be my preferred option, given a choice. 如果有选择,这将是我的首选。

public IEnumerable<string> Oz
{
    get { return (IEnumerable<string>)GetValue(OzProperty); }
    set { SetValue(OzProperty, value); }
}

public static readonly DependencyProperty OzProperty = DependencyProperty.Register("Oz", typeof(IEnumerable<string>), typeof(MainWindow), new PropertyMetadata(null));  

Its worth pointing out that if the container doesn't implement INotifyCollectionChanged (Such as ObservableCollection ) then you will only see updates to the ComboBox when the entire list is replaced with an assignment. 值得指出的是,如果容器未实现INotifyCollectionChanged (例如ObservableCollection ),则当整个列表被分配替换时,您只会看到ComboBox更新。

If you really can't use ObservableCollection you could notify in the get and set. 如果您确实不能使用ObservableCollection ,则可以在get和set中进行通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM