简体   繁体   中英

WPF Binding TextBox Text Property to a specific ViewModel's Property in a TreeView

I have a TreeView which I have bound to a ViewModel called RootViewModel which has a collection of ViewModels ChildViewModel which has a collection of ViewModels GrandhChildViewModel .

I have a text box next to the tree view.

I want to bind the text box's text to a specific property of the selected item when a GrandChildViewModel is selected in the tree. 将文本框的文本绑定到所选项目的特定属性。 If a GrandChildViewModel is not selected, I want the text in the textbox to be empty. I'd also like to have it so if I change the text in the TextBox when a GrandChild is selected it will update the text for that item in the treeview. (I'm assuming 2 way binding between the viewmodel<->textbox and viewmodel<->treeview

|------------------|
| - Root           |
|   - Child        |     |---------------------------------|
|     - GrandChild |     | Selected GrandChild's Property  |
|     - GrandChild |     |---------------------------------|
|     - GrandChild |                            ^
|   - Child        |                            |
|     - GrandChild |<----- Selected GrandChild -|
|                  |
|------------------|

Sample ViewModels:

public class RootViewModel : INotifyPropertyChanged
{
    public ObservableCollection<ChildViewModel> Children{ get; set; }
    public bool IsSelected{ get { /* code */ } set { /* code */ OnPropertyChanged("IsSelected"); }
}
public class ChildViewModel : INotifyPropertyChanged
{
    public ObservableCollection<GrandChildViewModel> GrandChildren{ get; set; }
    public bool IsSelected{ get { /* code */ } set { /* code */ OnPropertyChanged("IsSelected"); }
    public string SomeProperty{ get { /* code */ } set { /* code */ OnPropertyChanged("SomeProperty"); }
}
public class GrandChildViewModel : INotifyPropertyChanged
{
    public bool IsSelected{ get { /* code */ } set { /* code */ OnPropertyChanged("IsSelected"); }
    public bool SomeOtherProperty{ get { /* code */ } set { /* code */ OnPropertyChanged("SomeOtherProperty"); }
}

My attempt's so far in Xaml:

<TextBox>
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                &ltDataTrigger Value="True">
                    <DataTrigger.Binding>
                         <Binding Converter="{StaticResource myConverter}" ElementName="myTreeView" Path="SelectedItem" Mode="TwoWay" />
                    </DataTrigger.Binding>
                    <DataTrigger.Setters>
                        <Setter Property="Text" Value="{Binding Path=SomeOtherProperty}" />
                    </DataTrigger.Setters>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

Also tried

<TextBox Text={Binding ElementName=myTreeView, Path=SelectedItem, Converter={StaticResource myConverter}, Mode=TwoWay}" />

Many Thanks in advance.

I found that this is throwing an exception because TwoWay DataBinding on SelectedItem is not allowed.

Thanks to this post I was able to get this to work. I set the StackPanel around the text box to bind to the selected item, then bound the text box to the property I care about.

<StackPanel DataContext={Binding ElementName=myTreeView, Path=SelectedItem}">
    <TextBox Text={Binding Path=SomeOtherProperty, Mode=TwoWay}" />
</StackPanel>

Works like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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