简体   繁体   English

WPF:如何将OpenFileDialog的结果绑定到已经绑定的TextBox.Text

[英]WPF: How to bind the result of an OpenFileDialog to a TextBox.Text, which is already bound

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. 我有一个ListBox,它绑定到一个对象列表,在这个ListBox的DataTemplate中,我有一个TextBox,它绑定到这个对象的Property。 Now I have a Button in this DataTemplate, too, that opens an OpenFileDialog. 现在,我在此DataTemplate中也有一个Button,它会打开一个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. 我想将此OpenFileDialog的结果绑定到TextBox.Text,所以结果显示在TextBox中,并且绑定到此TextBox的对象的值更改为result。

The Xaml: 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: 绑定到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;
    }
}

If the Textbox has already bound to prop1 maybe it's better to just change prop1 value in order to change your textbox text. 如果Textbox已绑定到prop1 ,则最好更改prop1值以更改您的文本框文本。 such as

private void BtnOpen_OnClick(object sender, RoutedEventArgs e)
{
    OpenFileDialog fileDialog = new OpenFileDialog();
    fileDialog.Multiselect = false;

    dynamic result = fileDialog.ShowDialog();

    if (result == true) 
    {
        prop1=fileDialog.FileName; // set prop1 in the appropriate way.
    }
}

Consequently the textbox text will be changed. 因此,文本框文本将被更改。

Your class should implement INofifyPropertyChanged and your collection should implement IListChanged interface (like ObservableCollection or BindingList 您的类应实现INofifyPropertyChanged而集合应实现IListChanged接口(例如ObservableCollectionBindingList

If that's the case and you update your property the bound control will update its content. 如果是这种情况,并且您更新了属性,则绑定控件将更新其内容。

There are many ways to implement INotifyPropertyChanged. 有许多方法可以实现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));
    }

}

You can set a property value by using the base functionality: 您可以使用基本功能来设置属性值:

if (result == true) 
{
    txtName.SetValue(TextBox.TextProperty, fileDialog.FileName); 
}

I could have sworn txtPath.Text = fileDialog.FileName; 我可能宣誓txtPath.Text = fileDialog.FileName; would do this for you, but I don't have a compiler to test it right now. 会为您执行此操作,但是我现在没有编译器对其进行测试。

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

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