BindingToStringFormat

练习:

  1. gridview的列绑定string格式
  2. listbox文本的MultiBinding多绑定string格式

clipboard.png
clipboard.png

列绑定GridViewColumn.DisplayMemberBinding中的stringformat

<ListView ItemsSource="{StaticResource MyData}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Description}"/>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Price, StringFormat=Now {0:c}!}"/>
        </GridView>
    </ListView.View>
</ListView>

ListBox 的数据模板绑定中的stringformat

<ListBox ItemsSource="{StaticResource MyData}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding  StringFormat="{}{0} -- Now only {1:C}!">
                        <Binding Path="Description"/>
                        <Binding Path="Price"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

StringFormat 可以是预定义的、撰写的或自定义的字符串格式。

  1. 如果设置 Converter 和 StringFormat 属性,则会先对数据值应用转换器,然后应用 StringFormat。
  2. 当将 Binding 上的 StringFormat 设置为撰写字符串格式时,只能指定一个参数。
  3. 如果使用 MultiBinding,则仅当 StringFormat 属性是在 MultiBinding 上设置的时才应用此属性。

##BindValidation绑定验证##
练习点:

  1. AgeRangeRule : ValidationRule中的Validate
  2. Text中的Validation.ErrorTemplate控件
  3. 样式中提示工具的触发及内容

clipboard.png

clipboard.png

public int Min { get; set; }
public int Max { get; set; }

public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
    var age = 0;

    try
    {
        if (((string) value).Length > 0)
            age = int.Parse((string) value);
    }
    catch (Exception e)
    {
        return new ValidationResult(false, "Illegal characters or " + e.Message);
    }

    if ((age < Min) || (age > Max))
    {
        return new ValidationResult(false,
            "Please enter an age in the range: " + Min + " - " + Max + ".");
    }
    return new ValidationResult(true, null);
}
<Label Grid.Column="0" Grid.Row="1" FontSize="15" Margin="2"
   Target="{Binding ElementName=textBox1}">TextBox with _custom ErrorTemplate and ToolTip:</Label>
<TextBox Name="textBox1" Width="50" FontSize="15"
     Validation.ErrorTemplate="{StaticResource ValidationTemplate}"
     Style="{StaticResource TextBoxInError}"
     Grid.Row="1" Grid.Column="1" Margin="2">
    <TextBox.Text>
        <Binding Path="Age" Source="{StaticResource Ods}"
         UpdateSourceTrigger="PropertyChanged" >
            <Binding.ValidationRules>
                <local:AgeRangeRule Min="21" Max="130"/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

第二个TextBox

<Label Grid.Row="2" Grid.Column="0" FontSize="15" Margin="2"
   Target="{Binding ElementName=textBox2}">TextBox with _default ErrorTemplate:</Label>
<TextBox Name="textBox2" Width="50" FontSize="15"
     Grid.Row="2" Grid.Column="1" Margin="2">
    <TextBox.Text>
        <Binding Path="Age2" Source="{StaticResource Ods}"
         UpdateSourceTrigger="PropertyChanged" >
            <Binding.ValidationRules>
                <local:AgeRangeRule Min="21" Max="130"/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

第三个TextBox

<Label Grid.Row="4" Grid.Column="0" FontSize="15" Margin="2"
   Target="{Binding ElementName=textBox3}">TextBox with UpdateSourceExceptionFilter _handler:</Label>
<TextBox Name="textBox3" Width="50" FontSize="15"
     Grid.Row="4" Grid.Column="1" Margin="2"
     Validation.ErrorTemplate="{StaticResource ValidationTemplate}"
     Style="{StaticResource TextBoxInError}">
    <TextBox.Text>
        <Binding Path="Age3" Source="{StaticResource Ods}"
         UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <ExceptionValidationRule/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>
  1. 默认情况下,当出现验证错误时, TextBox 显示红色边框(第三个TextBox)。
  2. 可以选择编写自定义 ErrorTemplate,以指定当出现验证错误时如何通知用户(第一个TextBox)。
  3. ExceptionValidationRule 类是一个内置的规则,它检查在绑定源属性更新过程中引发的异常。

最后,提供自定义逻辑来处理那些异常。
练习:

  1. 获取绑定myBindingExpression 、Binding
  2. 附加自定义处理异常方法

代码如下:

private void UseCustomHandler(object sender, RoutedEventArgs e)
    {
        var myBindingExpression = textBox3.GetBindingExpression(TextBox.TextProperty);
        var myBinding = myBindingExpression.ParentBinding;
        myBinding.UpdateSourceExceptionFilter = ReturnExceptionHandler;
        myBindingExpression.UpdateSource();
    }
private void DisableCustomHandler(object sender, RoutedEventArgs e)
    {
        // textBox3 is an instance of a TextBox
        // the TextProperty is the data-bound dependency property
        var myBinding = BindingOperations.GetBinding(textBox3, TextBox.TextProperty);
        myBinding.UpdateSourceExceptionFilter -= ReturnExceptionHandler;
        BindingOperations.GetBindingExpression(textBox3, TextBox.TextProperty).UpdateSource();
    }

    private object ReturnExceptionHandler(object bindingExpression, Exception exception) => "This is from the UpdateSourceExceptionFilterCallBack.";

李志玮
22 声望34 粉丝

求索~~


引用和评论

0 条评论