WPF MultiBinding 绑定指定的value
1.MultiBinding
MultiBinding:多值转换器,继承于 System.Windows.Data.IMultiValueConverter接口,绑定多条数据组合为一个数组,按照需求处理数据,并返回需要的值
2.使用
public class MultiStatueConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
<Button Width="60" Height="40"> <Button.Visibility> <MultiBinding Converter="{StaticResource MultiStatueConverter}"> <Binding Path="DataContext.StartHostRounds" RelativeSource="{RelativeSource AncestorType=Window,Mode=FindAncestor}"/> <Binding Path="IsLoginUser"/> </MultiBinding> </Button.Visibility> </Button>
<Binding Path="IsLoginUser"/> 根据当前控件的DataContext查找源
Binding:指定需要绑定的数据源,可通过ElementName,RelativeSource,Source指定绑定的数据源。
ElementName:绑定到元素对象
<Binding Path="Content" ElementName="button"/>
RelativeSource:引用,使用RelativeSource对象指定源对象
<Binding Path="DataContext.StartHostRounds" RelativeSource="{RelativeSource AncestorType=Window,Mode=FindAncestor}"/>
Source:直接指向源对象
<Grid.Resources> <mydata:testData x:Key="testData " /> </Grid.Resources> <Binding Source="{StaticResource testData}" />