在 WPF 中使用自定义依赖属性作为 DataTrigger

发布于 2024-08-16 14:43:15 字数 2072 浏览 1 评论 0原文

我有一个自定义依赖属性,我想将其用作数据触发器。这是背后的代码:

public static readonly DependencyProperty BioinsulatorScannedProperty =
    DependencyProperty.Register(
        "BioinsulatorScanned", 
        typeof(bool), 
        typeof(DisposablesDisplay), 
        new FrameworkPropertyMetadata(false));

    public bool BioinsulatorScanned
    {
        get
        {
            return (bool)GetValue(BioinsulatorScannedProperty);
        }
        set
        {
            SetValue(BioinsulatorScannedProperty, value);
        }
    }

我创建了一个样式和控件模板。我的目标是当 dependency prop 设置为 true 时更改某些文本的颜色...

<Style x:Key="TreatEye" TargetType="Label">
        <Setter Property="Foreground" Value="#d1d1d1" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="30" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <Canvas>                            
                        <TextBlock x:Name="bioinsulatorText" 
                                   Canvas.Left="21" Canvas.Top="33" 
                                   Text="Bioinsulator" />
                        <TextBlock Canvas.Left="21" Canvas.Top="70" 
                                   Text="KXL Kit" />
                    </Canvas>

                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding BioinsulatorScanned}"
                                     Value="True">
                            <Setter TargetName="bioinsulatorText" 
                                    Property="Foreground" Value="Black" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>            
    </Style>

尽管以编程方式成功将 dependency prop 设置为 true,但此触发条件永远不会触发。调试起来真的很痛苦!

提前致谢。

I have a custom dependency property that I would like to use as a data trigger. Here is the code behind:

public static readonly DependencyProperty BioinsulatorScannedProperty =
    DependencyProperty.Register(
        "BioinsulatorScanned", 
        typeof(bool), 
        typeof(DisposablesDisplay), 
        new FrameworkPropertyMetadata(false));

    public bool BioinsulatorScanned
    {
        get
        {
            return (bool)GetValue(BioinsulatorScannedProperty);
        }
        set
        {
            SetValue(BioinsulatorScannedProperty, value);
        }
    }

I have created a style and control template. My goal is to change the color of some text when the dependency prop is set to true...

<Style x:Key="TreatEye" TargetType="Label">
        <Setter Property="Foreground" Value="#d1d1d1" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="30" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <Canvas>                            
                        <TextBlock x:Name="bioinsulatorText" 
                                   Canvas.Left="21" Canvas.Top="33" 
                                   Text="Bioinsulator" />
                        <TextBlock Canvas.Left="21" Canvas.Top="70" 
                                   Text="KXL Kit" />
                    </Canvas>

                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding BioinsulatorScanned}"
                                     Value="True">
                            <Setter TargetName="bioinsulatorText" 
                                    Property="Foreground" Value="Black" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>            
    </Style>

Despite successfully setting the dependency prop to true programmatically, This trigger condition never fires. This is a real pain to debug!

Thanks in advance.

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

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

发布评论

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

评论(2

ˉ厌 2024-08-23 14:43:15

在本例中,我使用基于依赖属性 FirstLevelProperty 的数据触发器来切换按钮的可见性。

public static readonly DependencyProperty FirstLevelProperty = DependencyProperty.Register("FirstLevel", typeof(string), typeof(MyWindowClass));

public string FirstLevel
        {
            get
            {
                return this.GetValue(FirstLevelProperty).ToString();
            }

            set
            {
                this.SetValue(FirstLevelProperty, value);
            }
        }

您可以使用 RelativeSource 绑定来引用窗口中包含(在本例中)的依赖属性 FirstLevel(Property)。此外,您还应该在样式中设置默认设置,该设置将被数据触发器覆盖。

<Button.Style>
    <Style TargetType="Button">
        <Style.Triggers>
            <DataTrigger 
             Binding="{Binding Path=FirstLevel,
             RelativeSource={RelativeSource FindAncestor,
             AncestorType={x:Type Window}}}" 
             Value="SomeValue">
                <Setter Property="Visibility" 
                 Value="Hidden" />
             </DataTrigger>
        </Style.Triggers>
        <Setter Property="Visibility" Value="Visible" />
    </Style>
</Button.Style>

In this case I am switching the visibility of a button using a datatrigger based on a dependency property FirstLevelProperty.

public static readonly DependencyProperty FirstLevelProperty = DependencyProperty.Register("FirstLevel", typeof(string), typeof(MyWindowClass));

public string FirstLevel
        {
            get
            {
                return this.GetValue(FirstLevelProperty).ToString();
            }

            set
            {
                this.SetValue(FirstLevelProperty, value);
            }
        }

You can reference the dependency property FirstLevel(Property) contained (in this case) in a window by using the RelativeSource binding. Also you should set the default setting in the style, that will be overridden by the datatrigger.

<Button.Style>
    <Style TargetType="Button">
        <Style.Triggers>
            <DataTrigger 
             Binding="{Binding Path=FirstLevel,
             RelativeSource={RelativeSource FindAncestor,
             AncestorType={x:Type Window}}}" 
             Value="SomeValue">
                <Setter Property="Visibility" 
                 Value="Hidden" />
             </DataTrigger>
        </Style.Triggers>
        <Setter Property="Visibility" Value="Visible" />
    </Style>
</Button.Style>
终止放荡 2024-08-23 14:43:15

看起来您的依赖属性是在您创建的 DisposableDisplay 对象内定义的。为了使指定的绑定起作用,必须将该 DisposableDisplay 对象的实例设置为控件(在本例中为标签)或其任何祖先的 DataContext

It looks like your dependency property is defined inside a DisposableDisplay object that you created. In order for the binding specified to work, an instance of that DisposableDisplay object must be set as the DataContext of the control (label in this case) or any of its ancestors.

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