3

MyUserControlというカスタムユーザーコントロールを作成しています。いくつかのMyUserControlが複数回定義されているメインウィンドウで使用するDependecyPropertiesがたくさんあります。私が知りたいのは、スタイルのトリガー/プロパティが起動するカスタムプロパティを作成するにはどうすればよいですか?

たとえば、カスタムプロパティBOOL IsGoingとカスタムプロパティMyBackgroung(UserControlの背景)がある場合、どちらも次のように定義されます。

public bool IsGoing
    {
        get { return (bool)this.GetValue(IsGoingProperty); }
        set { this.SetValue(IsGoingProperty, value); }
    }
    public static readonly DependencyProperty IsGoingProperty = DependencyProperty.RegisterAttached(
        "IsGoing", typeof(bool), typeof(MyUserControl), new PropertyMetadata(false));  

public Brush MyBackground
    {
        get { return (Brush)this.GetValue(MyBackgroundProperty); }
        set { this.SetValue(MyBackgroundProperty, value); }
    }
    public static readonly DependencyProperty MyBackgroundProperty = DependencyProperty.Register(
            "MyBackground", typeof(Brush), typeof(MyUserControl), new PropertyMetadata(Brushes.Red));

また、MainWindow.xamlでUserControlを定義した場合、IsGoingプロパティがtrue / falseであるかどうかに応じて、トリガーにアクセスしてMyBackgroundを設定するにはどうすればよいですか?私は多くのことを試みましたが、本質的には、次のようなことを達成しようとしています。

<custom:MyUserControl MyBackground="Green" x:Name="myUC1" Margin="120.433,0,0,65.5" Height="50" Width="250" VerticalAlignment="Bottom" HorizontalAlignment="Left"  >
        <Style>
            <Style.Triggers>
                <Trigger Property="IsGoing" Value="True">
                    <Setter Property="MyBackground" Value="Yellow"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </custom:MyUserControl>

私の説明があなたが理解するのに十分良いことを願っています。私はこれに数日間取り組んできましたが、解決策が見つからないようです。助けてくれてありがとう!!!

エイドリアン

4

1 に答える 1

3

あなたのスタイルはUserControl.Style正しいものとして使用する必要がありますTargetType。また、トリガーを介して変更する予定のデフォルト値は、優先順位のためにスタイルに移動する必要があります:

<custom:MyUserControl.Style>
    <Style TargetType="custom:MyUserControl">
        <Setter Property="MyBackground" Value="Green"/>
        <Style.Triggers>
            <Trigger Property="IsGoing" Value="True">
                <Setter Property="MyBackground" Value="Yellow"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</custom:MyUserControl.Style>

これが実際に何かを行うかどうかは、コントロール定義でプロパティをどのように使用するかによって異なります。

于 2012-02-26T17:47:51.507 に答える