我有一个ListBox,它绑定到一个对象列表,在这个ListBox的DataTemplate中我有一个TextBox,它绑定到这个对象的Property。 现在我在这个DataTemplate中也有一个Button,它打开一个OpenFileDialog。 我想将此OpenFileDialog的结果绑定到TextBox.Text,因此结果显示在TextBox中,并且绑定到此TextBox的对象的值更改为result。
Xaml:
<ListBox Name="MyList"> <ListBox.ItemTemplate> <DataTemplate> <DockPanel> <Button Name="btnOpen" Click="BtnOpen_OnClick"/> <TextBox Name="txtPath" Text="{Binding Path=Prop2, Mode=TwoWay}"/> </DockPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>守则背后:
private void BtnOpen_OnClick(object sender, RoutedEventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Multiselect = false; dynamic result = fileDialog.ShowDialog(); if (result == true) { //bind to TextBox textproperty here } }列表中绑定到ListBox的对象的结构如下:
public class Item { public string Prop1 { get; set; } public string Prop2 { get; set; } public bool Prop3 { get; set; } public Item(string prop1) { this.Prop1 = prop1; } public Item(string prop1, string prop2) { this.Prop1 = prop1; this.Prop2 = prop2; } public Item(string prop1, string prop2, bool prop3) { this.Prop1 = prop1; this.Prop2 = prop2; this.Prop3 = prop3; } }I have a ListBox, which is bound to a list of objects and in the DataTemplate of this ListBox I have a TextBox, which is bound to a Property of this objects. Now I have a Button in this DataTemplate, too, that opens an OpenFileDialog. I want to bind the result of this OpenFileDialog to the TextBox.Text, so the result is shown in the TextBox and the value of the object, which is bound to this TextBox changes to result.
The Xaml:
<ListBox Name="MyList"> <ListBox.ItemTemplate> <DataTemplate> <DockPanel> <Button Name="btnOpen" Click="BtnOpen_OnClick"/> <TextBox Name="txtPath" Text="{Binding Path=Prop2, Mode=TwoWay}"/> </DockPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>The Code Behind:
private void BtnOpen_OnClick(object sender, RoutedEventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Multiselect = false; dynamic result = fileDialog.ShowDialog(); if (result == true) { //bind to TextBox textproperty here } }The objects in the list that is bound to the ListBox, are structured as follows:
public class Item { public string Prop1 { get; set; } public string Prop2 { get; set; } public bool Prop3 { get; set; } public Item(string prop1) { this.Prop1 = prop1; } public Item(string prop1, string prop2) { this.Prop1 = prop1; this.Prop2 = prop2; } public Item(string prop1, string prop2, bool prop3) { this.Prop1 = prop1; this.Prop2 = prop2; this.Prop3 = prop3; } }最满意答案
您的类应该实现INofifyPropertyChanged ,您的集合应该实现IListChanged接口(如ObservableCollection或BindingList
如果是这种情况并且您更新了属性,则绑定控件将更新其内容。
有许多方法可以实现INotifyPropertyChanged。 最快的解决方案是:
public class Item : INotifyPropertyChanged { private string prop2; public string Prop2 { get { return prop2; } set { prop2 = value; OnPropertyChanged("Prop2"); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { var eh = this.PropertyChanged; if (eh != null) eh(this, new PropertyChangedEventArgs(propertyName)); } }Your class should implement INofifyPropertyChanged and your collection should implement IListChanged interface (like ObservableCollection or BindingList
If that's the case and you update your property the bound control will update its content.
There are many ways to implement INotifyPropertyChanged. The quickest solution is this:
public class Item : INotifyPropertyChanged { private string prop2; public string Prop2 { get { return prop2; } set { prop2 = value; OnPropertyChanged("Prop2"); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { var eh = this.PropertyChanged; if (eh != null) eh(this, new PropertyChangedEventArgs(propertyName)); } }更多推荐
TextBox,result,ListBox,OpenFileDialog,电脑培训,计算机培训,IT培训"/> <meta na
发布评论