在WPF中将自定义依赖项属性用作DataTrigger

时间:2010-01-04 20:58:16

标签: wpf properties dependencies datatrigger

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

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);
        }
    }

我创建了一个样式和控件模板。我的目标是在依赖项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>

尽管以编程方式成功将依赖项prop设置为true,但此触发器条件永远不会触发。这是一个真正的调试痛苦!

提前致谢。

2 个答案:

答案 0 :(得分:3)

在这种情况下,我使用基于依赖属性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);
            }
        }

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

<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>

答案 1 :(得分:2)

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