WPF 使用 DependencyProperty 更改 UserControl 中标签的背景

发布于 2024-09-15 09:10:09 字数 1258 浏览 6 评论 0原文

我有一个非常简单的用户控件,如下所示。我试图在控件中的属性发生更改时更改 Label 元素的背景,但它不起作用:当我更改控件实例上的 Selected 属性时,标签的背景颜色不会更改。

谢谢!

背后代码:

    public static readonly DependencyProperty SelectedProperty =
            DependencyProperty.Register("Selected",
            typeof(bool),
            typeof(UICatcherContactlistItem),
            new FrameworkPropertyMetadata((bool)false));

    public bool Selected
    {
        get { return (bool)GetValue(SelectedProperty); }
        set { SetValue(SelectedProperty, value); }
    }

Xaml:

<UserControl x:Class="UICatcherContactlistItem" [....]> 
    <Label Name="name" Foreground="#888888">
        <Style TargetType="{x:Type Label}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Selected}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Selected}" Value="False">
                    <Setter Property="Background" Value="Blue"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Label>    
</UserControl>

I have a very simple UserControl as shown below. I'm trying to get the background of the Label element to change whenever a property in the control changes, but it's not working: when I change the Selected property on the control instance, the label's background color does not change.

Thanks!

Code behind:

    public static readonly DependencyProperty SelectedProperty =
            DependencyProperty.Register("Selected",
            typeof(bool),
            typeof(UICatcherContactlistItem),
            new FrameworkPropertyMetadata((bool)false));

    public bool Selected
    {
        get { return (bool)GetValue(SelectedProperty); }
        set { SetValue(SelectedProperty, value); }
    }

Xaml:

<UserControl x:Class="UICatcherContactlistItem" [....]> 
    <Label Name="name" Foreground="#888888">
        <Style TargetType="{x:Type Label}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Selected}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Selected}" Value="False">
                    <Setter Property="Background" Value="Blue"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Label>    
</UserControl>

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

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

发布评论

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

评论(1

御守 2024-09-22 09:10:09

只需给出 UserControl 名称即可执行此代码,此处我使用 test。
如果您在用户控件上使用 Dependency 属性,则可以通过 ElementName 属性访问该属性,或者必须为元素设置 Datacontext,如 name.DataContext=this..

<Label Name="name" Foreground="#888888" Content="text" Height="100" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" >
        <Label.Style>

        <Style TargetType="{x:Type Label}">
            <!--<Setter Property="Background" Value="Yellow"/>-->
            <Style.Triggers>
                <DataTrigger Binding="{Binding Selected,ElementName=test}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Selected,ElementName=test}" Value="False">
                    <Setter Property="Background" Value="Blue"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        </Label.Style>
    </Label>

just give the UserControl name to execute this code, here iam using test.
If you are using Dependency property on the usercontrol you can access the property by either ElementName property or you have to set the Datacontext for the element like name.DataContext=this..

<Label Name="name" Foreground="#888888" Content="text" Height="100" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" >
        <Label.Style>

        <Style TargetType="{x:Type Label}">
            <!--<Setter Property="Background" Value="Yellow"/>-->
            <Style.Triggers>
                <DataTrigger Binding="{Binding Selected,ElementName=test}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Selected,ElementName=test}" Value="False">
                    <Setter Property="Background" Value="Blue"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        </Label.Style>
    </Label>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文