WPF:如何将OpenFileDialog的结果绑定到已绑定的TextBox.Text(WPF: How to bind the result of an OpenFileDialog to a Te

编程入门 行业动态 更新时间:2024-10-28 14:36:43
WPF:如何将OpenFileDialog的结果绑定到已绑定的TextBox.Text(WPF: How to bind the result of an OpenFileDialog to a TextBox.Text, which is already bound)

我有一个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

本文发布于:2023-08-03 04:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1383203.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   如何将   OpenFileDialog   WPF   TextBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!