在 WPF 中使用 DataTrigger

发布于 2024-11-05 06:06:04 字数 1236 浏览 1 评论 0原文

我有一个在 XAML 中定义的 TextBox 控件,我想根据其 IsReadOnly 或 IsEnabled 属性将不同的背景颜色应用于 TextBox。我使用 dataTriggers 实际上在颜色之间切换,如下所示:

<Style x:Key="TextBoxStyle" TargetType="TextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsEnabled}" Value="True">
            <Setter Property="TextBox.Background" Value="Yellow"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding IsReadOnly}" Value="True">
            <Setter Property="TextBox.Background" Value="Red"/>
        </DataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsReadOnly}" Value="True"/>
                <Condition Binding="{Binding IsEnabled}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="Green"/>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

并且 TextBox 的定义如下所示:

  <TextBox Name="sourceTextBox"  Margin="5,3,5,3" IsReadOnly="True" Style="{StaticResource TextBoxStyle}" />

但问题是,颜色没有正确应用。

上面的做法有问题吗?

I have a TextBox control defined in XAML and I want to apply different background colors to the TextBox based on its IsReadOnly or IsEnabled properties. I used dataTriggers to actually switch between the colors as shown below:

<Style x:Key="TextBoxStyle" TargetType="TextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsEnabled}" Value="True">
            <Setter Property="TextBox.Background" Value="Yellow"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding IsReadOnly}" Value="True">
            <Setter Property="TextBox.Background" Value="Red"/>
        </DataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsReadOnly}" Value="True"/>
                <Condition Binding="{Binding IsEnabled}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="Green"/>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

And the TextBox is defined as shown below:

  <TextBox Name="sourceTextBox"  Margin="5,3,5,3" IsReadOnly="True" Style="{StaticResource TextBoxStyle}" />

But the problem is, the colors are not being applied properly.

Is there any problem with the above approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

度的依靠╰つ 2024-11-12 06:06:04

你让事情变得不必要地复杂化

<Style x:Key="TextBoxStyle" TargetType="TextBox">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="True">
            <Setter Property="Background" Value="Yellow"/>
        </Trigger>
        <Trigger Property="IsReadOnly" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsEnabled" Value="True"/>
                <Condition Property="IsReadOnly" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" Value="Green"/>
        </MultiTrigger>
    </Style.Triggers>
</Style>

You are unecessarily complicating things

<Style x:Key="TextBoxStyle" TargetType="TextBox">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="True">
            <Setter Property="Background" Value="Yellow"/>
        </Trigger>
        <Trigger Property="IsReadOnly" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsEnabled" Value="True"/>
                <Condition Property="IsReadOnly" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" Value="Green"/>
        </MultiTrigger>
    </Style.Triggers>
</Style>
浸婚纱 2024-11-12 06:06:04

我认为您只需要添加 RelativeSource={RelativeSource Self} 到您的绑定中:

<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="True">
            <Setter Property="Background" Value="Yellow" />
        </DataTrigger>
        <DataTrigger Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="True">
            <Setter Property="Background" Value="Red" />
        </DataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="True"/>
                <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="Green"/>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

但是仍然存在一个问题,我不相信您会看到红色背景,因为带有 IsEnabled 的 TextBox设置为 False 的属性在其控件模板中具有内置背景颜色,该背景颜色将优先于样式触发器的设置器。

我认为当 TextBox 被禁用时,您必须重新定义其控件模板才能更改背景颜色。

I think you need just need to add RelativeSource={RelativeSource Self} to your bindings:

<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="True">
            <Setter Property="Background" Value="Yellow" />
        </DataTrigger>
        <DataTrigger Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="True">
            <Setter Property="Background" Value="Red" />
        </DataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="True"/>
                <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="Green"/>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

There is still one problem however, I don't believe you will ever see a Red background because a TextBox with its IsEnabled property set to False has a built in background color into its control template that will take priority over your style's trigger's setter.

I think you'd have to redefine its control template to change the background color when the TextBox is disabled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文