簡體   English   中英

Datatriggers在子控件的樣式中訪問和設置UserControl的自定義屬性

[英]Datatriggers access and set custom Properties of a UserControl within the style of a child Control

我有一個UserControl有兩個自定義屬性CustomACustomB 我想在UserControl Label控件中使用DataTriggers來更改這些自定義屬性的Value

在我的例子,我似乎沒有或不知道如何訪問可以CustomB在setter屬性,這樣我就可以改變它的ValueValueCustomA物業在DataTrigger變化。 我認為CustomA Property的綁定是正確的,但我不知道如何使用Setter來訪問CustomB

總而言之,我需要知道如何從UserControl的Style DataTriggers中訪問屬於我的UserControl自定義屬性 - 在本例中為Label - 並且還可以更改它們的值

UCLabel.xaml - UserControl

<UserControl x:Class="UCLabel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:TestProgram"
         mc:Ignorable="d"
         d:DesignHeight="30" d:DesignWidth="100">

<Label Name="lbl">
    <Label.Style>
        <Style TargetType="{x:Type Label}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=CustomA}" Value="True">
                    <Setter Property="CustomB" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Label.Style>
</Label>
</UserControl>

UCLabel.xaml.vb - 代碼背后

Imports System.Windows
Public Class UCLabel

  'CustomA'
  Public Shared ReadOnly CustomAProperty As DependencyProperty =
        DependencyProperty.Register("CustomA",
                                    GetType(Boolean),
                                    GetType(UCLabel), New PropertyMetadata(False))

  Public Property CustomA As Boolean
    Get
        Return CBool(GetValue(CustomAProperty))
    End Get
    Set(ByVal value As Boolean)
        SetValue(CustomAProperty, value)
    End Set
  End Property

  'CustomB'
  Public Shared ReadOnly CustomBProperty As DependencyProperty =
        DependencyProperty.Register("CustomB",
                                    GetType(Boolean),
                                    GetType(UCLabel), New PropertyMetadata(False))

  Public Property CustomB As Boolean
    Get
        Return CBool(GetValue(CustomBProperty))
    End Get
    Set(ByVal value As Boolean)
        SetValue(CustomBProperty, value)
    End Set
  End Property
End Class

在樣式中使用Setter時,意味着更改當前控件的屬性(在您的情況下,它是Label),並且不能更改任何其他控件的屬性。 但是,您可以通過“ Behaviors實現目標。

首先,您需要將以下程序集引用添加到項目中:

System.Windows.Interactivity
Microsoft.Expression.Interactions

然后使用以下代碼:

<UserControl
    x:Name="uc"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

    <Label>
        <i:Interaction.Triggers>
            <ei:DataTrigger Binding="{Binding ProA, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Value="True">
                <ei:ChangePropertyAction
                    PropertyName="ProB"
                    TargetName="uc"
                    Value="True" />
            </ei:DataTrigger>
        </i:Interaction.Triggers>
    </Label>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM