Binding常用辅助属性、多重绑定、优先级绑定
Binding常用辅助属性
- StringFormat
<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上。
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中使用多重绑定
<!--MultiBinding-->
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}-{1}-{2}">
<Binding Path="Value1"/>
<Binding Path="Value2"/>
<Binding Path="Value3" FallbackValue="异常"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
另一种实现方式
<!--两一种实现方式-->
<TextBlock>
<Run Text="{Binding Value1}"/><Run Text="{Binding Value2 ,StringFormat=-{0}}"/><Run Text="{Binding Value3 ,StringFormat=-{0}}"/>
</TextBlock>
使用IMultiValueConverter
// 多重绑定值转换器
public class MyValueConverter : IMultiValueConverter
{
/// <summary>
/// 从源到目标
/// </summary>
/// <param name="values">数组:绑定的所有值</param>
/// <param name="targetType">目标控件的目标属性</param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values[0].ToString() +"-"+ values[1].ToString() +"-IMultiValueConverter";
}
/// <summary>
/// 从目标到源
/// </summary>
/// <param name="value">单值</param>
/// <param name="targetTypes">类型数组</param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns>返回数组</returns>
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return null;
}
}
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource myConverter}">
<Binding Path="Value1"/>
<Binding Path="Value2"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
多重绑定转换器的详细说明参见5-3Binding对数据的转换和校验
三种方式的展示效果
优先级绑定
绑定是确定优先顺序
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
<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会先拿到值,但是优先级高的属性有值时仍然会显示优先级较高的属性内容。如果优先级较高的属性因为异常等原因不能显示时,会显示优先级较低的属性。