WPF新手之控件多对一的绑定

用到MultiBinding和IMultiValueConverter:

<Window.Resources> <local:HasSelectionNICAndProfileConverter x:Key="HasSelectionNICAndProfileConverter"/> </Window.Resources> <ListBox x:Name="ListBox1" ItemsSource="{Binding}"/> <TreeView x:Name="Tree1" ItemsSource="{Binding }" ItemTemplate="{StaticResource TemplateX}"/> <TextBox x:Name="ThyBox" > <TextBox.Text> <MultiBinding Converter="{StaticResource HasSelectionNICAndProfileConverter}" Mode="OneWay"> <Binding ElementName="ListBox1" Path="SelectedItem"/> <Binding ElementName="Tree1" Path="SelectedItem"/> </MultiBinding> </TextBox.Text> </TextBox>

多重绑定需要多值转换器:

public class HasSelectionNICAndProfileConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { object NICSelection = values[0]; object ProfileSelection = values[1]; if (NICSelection != null && ProfileSelection != null) return "Selected"; else return "UNSelected"; } public object[] ConvertBack(object values, Type[] targetType, object parameter, System.Globalization.CultureInfo culture) { return null; } }

这里的代码实现了,只有当两个TreeView同时有项被选中时,TextBlock中才会显示Selected。

你可能感兴趣的:(object,tree,null,Path,WPF,binding)