C# WPF:组合框数据绑定问题
我有一个C# WPF:组合框数据绑定问题,c#,wpf,data-binding,C#,Wpf,Data Binding,我有一个公共类LPosition,它包含一个属性公共字符串OZ{get;set;}。应用程序读取.txt和.xml文件,OZ从这些文件中获取值。我需要将OZ绑定到comboBox: <ComboBox x:Name="OZs" SelectionChanged="OZs_SelectionChanged" Margin="-10,0,10,1" Grid.Column="0" Grid.Row="1" Height="27" VerticalAlignment="Bottom">
公共类LPosition
,它包含一个属性公共字符串OZ{get;set;}
。应用程序读取.txt和.xml文件,OZ从这些文件中获取值。我需要将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>
如您所见,我曾尝试使用DataTemplate实现数据绑定,但它不起作用。我知道可以使用ItemsSource实现数据绑定,但为此,我需要一个ObservableCollection,我没有,也不知道如何从OZ创建它。我看到过很多例子,其中ObservableCollection是硬编码的,我知道在硬编码时如何使用它,但我不知道在我的情况下该怎么做。对不起,如果解释不清楚,我是WPF的新手。我对这个问题很迷茫。任何帮助都将不胜感激 编辑:根据@Xiaoy312的回答,我添加了以下代码:
public IEnumerable<LPosition> OZList { get; set; }
public FormelAssistent()
{
InitializeComponent();
this.DataContext = this;
OZs.ItemsSource = OZList;
}
public IEnumerable列表{get;set;}
公共行政助理
{
初始化组件();
this.DataContext=this;
OZs.ItemsSource=OZList;
}
和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" />
但它不起作用:(您需要提供一个内容列表(选项)通过其
ItemsSource
属性选择ComboBox
。您不必使用observedcollection
对其进行备份。仅当您计划在显示视图时添加/删除集合时,才可以使用任何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 >
您需要提供内容列表(选项)通过其ItemsSource
属性选择ComboBox
。您不必使用observedcollection
对其进行备份。仅当您计划在显示视图时添加/删除集合时,才可以使用任何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 >
如果您像这样在类上实现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");
}
}
public event propertychangedventhandler PropertyChanged;
私有void NotifyPropertyChanged(字符串PropertyName=“”)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(PropertyName));
}
}
私有的IEnumerable_oz;
公共数字盎司
{
得到
{
返回_oz;
}
设置
{
_oz=价值;
通知财产变更(“Oz”);
}
}
如果您碰巧使用了一个依赖项对象,您可以改为使用依赖项属性。如果有选择,这将是我的首选选项
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));
public IEnumerable Oz
{
获取{return(IEnumerable)GetValue(OzProperty);}
set{SetValue(属性,值);}
}
public static readonly dependencProperty OzProperty=dependencProperty.Register(“Oz”、typeof(IEnumerable)、typeof(MainWindow)、newPropertyMetadata(null));
值得指出的是,如果容器没有实现INotifyCollectionChanged
(例如observedcollection
),那么当整个列表被赋值替换时,您将只看到组合框的更新
如果您真的不能使用ObservableCollection
,您可以在get和set中进行通知。如果您在类上这样实现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");
}
}
public event propertychangedventhandler PropertyChanged;
私有void NotifyPropertyChanged(字符串PropertyName=“”)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(PropertyName));
}
}
私有的IEnumerable_oz;
公共数字盎司
{
得到
{
返回_oz;
}
设置
{
_oz=价值;
通知财产变更(“Oz”);
}
}
如果您碰巧使用了一个依赖项对象,您可以改为使用依赖项属性。如果有选择,这将是我的首选选项
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));
public IEnumerable Oz
{
获取{return(IEnumerable)GetValue(OzProperty);}
set{SetValue(属性,值);}
}
public static readonly dependencProperty OzProperty=dependencProperty.Register(“Oz”、typeof(IEnumerable)、typeof(MainWindow)、newPropertyMetadata(null));
值得指出的是,如果容器没有实现INotifyCollectionChanged
(例如observedcollection
),那么当整个列表被赋值替换时,您将只看到组合框的更新
如果您真的不能使用ObservableCollection
,您可以在get and set中通知。您不需要ObservableCollection将ItemsSource属性绑定到。任何集合都可以。但是,它应该是一个集合,而不是字符串。您可能应该解释OZ到底是什么,以及组合框应该显示什么。@Clemens OZ i是一个字符串,它是类LPosition的属性之一。必须显示的是OZ值的列表(它可以是例如“1.01”或“1.1.01”等,从文件中读取)。您没有使用依赖项属性、任何类型的可观察容器,也没有使用INotifyPropertyChanged参与观察者模式。绑定不轮询对象的状态更改,它们希望收到更改通知。您不需要ObservableCollection将ItemsSource属性绑定到。任何集合都可以。Howe无论如何,它应该是一个集合,而不是一个字符串。您可能应该解释OZ到底是什么,以及组合框应该显示什么。@Clemens OZ是一个字符串,它是LPosition类的属性之一。必须显示的是OZ值的列表(例如,它可以是“1.01”或“1.1.01”等,它是从文件中读取的)。您没有使用依赖属性、任何类型的可观察容器,也没有使用INotifyPropertyChanged参与观察者模式。绑定不会轮询对象的状态更改,它们希望收到更改通知。抱歉,