【问题标题】:Generic style for textboxes with Text binding in wpf using Triggers使用触发器在 wpf 中具有文本绑定的文本框的通用样式
【发布时间】:2014-10-07 02:09:03
【问题描述】:

我有一个要求,其中 TextBox 使用 ViewModel 绑定到属性。默认值为-1,但我不想向用户显示为-1,而是显示为“默认”。我的项目中很多地方都有类似的文本框。因此,创建了一种样式并在 DataTrigger 中设置了 Text 属性,但不知何故,代码无法正常工作。 我还在学习 Wpf。

请帮忙。

xaml如下。

<Window x:Class="TextBoxDefaultStyles.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TextBoxDefaultStyles"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>

    <Style TargetType="TextBox"
           x:Key="DefaultStyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Self}}" Value="-1">
                <Setter Property="Text" Value="Default"/>
                <Setter Property="Foreground" Value="Gray"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<StackPanel>
    <TextBox Text="{Binding Text}"
             Style="{StaticResource DefaultStyle}"/>
    <TextBox Text="Dummy"/>
</StackPanel>

我想要这种行为,如果 Binding 值为 -1,则将 Default 显示为文本,或者如果 Binding 值为 -1,则显示该文本。如果用户在编辑框中输入文本,通常是数字,则必须将其更新到底层绑定。

【问题讨论】:

  • 你绑定的属性是字符串还是其他类型?
  • 您必须从 DataTrigger 的绑定中删除 RelativeSource。它仍然只会设置Foreground 属性,但不会替换Text。只需使用绑定转换器即可。
  • 这只是绑定库中FallbackValue 的工作吗?
  • @LeeO。绑定到整数属性类型。

标签: c# wpf xaml binding datatrigger


【解决方案1】:

由于 WPF 中使用了Dependency Property Value Precedence 列表,您尝试执行的操作将无法正常工作。 DependencyPropertys 可以从多个不同的来源设置,因此 Microsoft 必须设计一个值优先级列表......哪个来源应该具有更高的优先级?

如果您查看链接页面,您将看到依赖属性设置优先级列表,其中显示了哪些源优先于其他源。您要做的是用Style Trigger 设置的值覆盖由数据绑定设置的值(列表中的本地值)。您应该注意到 Style Triggers 条目在列表中远低于 Local Value 条目。

这意味着它比 Local Value 条目的优先级更少,因此它永远不会覆盖该值。

相反,更常见的是使用IValueConverter Interface-1 值转换为Default

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (string.IsNullOrEmpty(value)) return DependencyProperty.UnsetValue;
    return value.ToString() == "-1" ? "Default" : value;
}

【讨论】:

  • +1 表示依赖属性值优先级。谢谢
【解决方案2】:

这不起作用,因为 Local Value 的优先级高于 Trigger Value 。 Dependency Property Precedence

使用转换器试试

xaml

<Window x:Class="Stackoverflow.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300"
    xmlns:local="clr-namespace:Stackoverflow"
    >
<Window.Resources>
    <local:TextConverter x:Key="textConverter"/>
</Window.Resources>

<Grid>
    <TextBox Text="{Binding Name, Converter=textConverter}"/>
</Grid>

转换器

    public class TextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null && value.ToString() == "-1")
            return "Default";
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

【讨论】:

  • 但是 Foreground 属性呢?我是否需要使用另一个转换器来检查 Text 是默认值还是实际值。
  • 对 Foreground 属性使用触发器,就像在您自己的示例中一样。不要检查 Text 是否为 -1,而是检查它是否为“默认”。
  • 是的,前台使用触发器,但在触发器中设置值 = 默认值而不是 -1。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-27
  • 1970-01-01
  • 2011-03-29
  • 2011-06-03
  • 1970-01-01
  • 2011-10-23
  • 2012-11-23
相关资源
最近更新 更多