Binding常用辅助属性、多重绑定、优先级绑定
Binding常用辅助属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <Window.Resources> <sys:Int32 x:Key="myInt">200</sys:Int32> <sys:Single x:Key="mySingle">100.123456</sys:Single> </Window.Resources> <StackPanel> <TextBlock Text="{Binding Source={StaticResource myInt}, StringFormat={}{0:C}}"/> <TextBlock Text="{Binding Source={StaticResource myInt}, StringFormat={}{0:C} ,ConverterCulture=zh-CN}"/> <TextBlock Text="{Binding Source={StaticResource myInt}, StringFormat=单价:{0:C} ,ConverterCulture=zh-CN}"/> <TextBlock Text="{Binding Source={StaticResource mySingle}, StringFormat={}{0:F2}}"/> <TextBlock Text="{Binding Path=DateTimeDateTime, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}, StringFormat={}{0:yyyy-MM-dd HH:mm:ss}}"/> </StackPanel>
|

FallbackValue
:bingding无法返回值时所显示的默认值TargetNullValue
:当绑定源为null时,绑定目标所显示的值Delay
:从绑定目标修改到绑定源更新的延迟时间
多重绑定
当需要的信息不止一个数据源时,可以使用MultiBinding,MultiBinding具有一个Bindings的属性,类型是Collection,处在这个集合中的Binding对象可以拥有自己的数据校验和转换机制,他们汇总起来的数据将传递到Target上。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class Data { private int _value1 =2;
public int Value1 { get { return _value1; } set { _value1 = value; } }
private int _value2 =2;
public int Value2 { get { return _value2; } set { _value2 = value; } }
private int _value3;
public int Value3 { get { return Value1 + Value2; } set { _value3 = value; } }
}
|
在XAML中使用多重绑定
1 2 3 4 5 6 7 8 9 10
| <TextBlock> <TextBlock.Text> <MultiBinding StringFormat="{}{0}-{1}-{2}"> <Binding Path="Value1"/> <Binding Path="Value2"/> <Binding Path="Value3" FallbackValue="异常"/> </MultiBinding> </TextBlock.Text> </TextBlock>
|
另一种实现方式
1 2 3 4
| <TextBlock> <Run Text="{Binding Value1}"/><Run Text="{Binding Value2 ,StringFormat=-{0}}"/><Run Text="{Binding Value3 ,StringFormat=-{0}}"/> </TextBlock>
|
使用IMultiValueConverter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public class MyValueConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { return values[0].ToString() +"-"+ values[1].ToString() +"-IMultiValueConverter"; }
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { return null; } }
|
1 2 3 4 5 6 7 8
| <TextBlock> <TextBlock.Text> <MultiBinding Converter="{StaticResource myConverter}"> <Binding Path="Value1"/> <Binding Path="Value2"/> </MultiBinding> </TextBlock.Text> </TextBlock>
|
多重绑定转换器的详细说明参见5-3Binding对数据的转换和校验
三种方式的展示效果

优先级绑定
绑定是确定优先顺序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class Data { private int _value1 = 1;
public int Value1 { get { Thread.Sleep(10000); return _value1; } set { _value1 = value; } }
private int _value2 = 2;
public int Value2 { get { Thread.Sleep(8000); return _value2; } set { _value2 = value; } }
private int _value3 = 3;
public int Value3 { get { Thread.Sleep(5000); return _value3; } set { _value3 = value; } } }
|
XAML
1 2 3 4 5 6 7 8
| <TextBox> <PriorityBinding FallbackValue="正在获取数据..."> <Binding Path="Value1" IsAsync="True" UpdateSourceTrigger="PropertyChanged"/> <Binding Path="Value2" IsAsync="True" UpdateSourceTrigger="PropertyChanged"/> <Binding Path="Value3" IsAsync="True" UpdateSourceTrigger="PropertyChanged"/> </PriorityBinding> </TextBox>
|
该案例中,优先级从高到低是Value1>Value2>Value3,虽然Value3会先拿到值,但是优先级高的属性有值时仍然会显示优先级较高的属性内容。如果优先级较高的属性因为异常等原因不能显示时,会显示优先级较低的属性。
