0

私は持っていUserControlます:

<UserControl x:Class="WpfApplication1.UserControl1"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Button Name="myButton" Content="hello world" Padding="10" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</UserControl>

および分離コード:

public partial class UserControl1 : UserControl
{
    public bool IsShown
    {
        get { return (bool)GetValue(IsShownProperty); }
        set { SetValue(IsShownProperty, value); }
    } 

    public static readonly DependencyProperty IsShownProperty =
        DependencyProperty.Register("IsShown", typeof(bool), typeof(UserControl1), new UIPropertyMetadata(false));

    public UserControl1()
    {
        InitializeComponent();
    }
}

IsShown が true のときに myButton に Effect を追加したい。メインウィンドウで、

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication1">
    <Window.Resources>
        <DropShadowEffect x:Key="outerGlow" Color="#00E300" Direction="0" ShadowDepth="0" BlurRadius="12" />
        <Style x:Key="style" TargetType="my:UserControl1">
            <Style.Triggers>
                <Trigger Property="IsShown" Value="true">
                    <Setter Property="myButton.Effect" Value="{StaticResource outerGlow}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <my:UserControl1 x:Name="userControl11" Style="{StaticResource style}" />
</Window>

しかし、VS は symbol を解決できませんmyButton

修正方法は?

アップデート:

MSDN の記事を見つけました。私のプロパティパスがどのカテゴリに該当するか誰か教えてもらえますか?

4

2 に答える 2

1

そんなことはできません。

UserControl のスコープ内にあるコントロールに影響を与える必要がある場合は、UserControl でプロパティを定義し、RelativeSource または ElementName を使用して内部コントロールでそれを参照する必要があります。

<UserControl x:Class="WpfApplication1.UserControl1"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" x:Name="mycontrol">
    <Button Name="myButton" Content="hello world" Padding="10" VerticalAlignment="Center" HorizontalAlignment="Center">
       <Button.Style>
          <Style>
             <Style.Triggers>
                <DataTrigger Binding="{Binding IsShown, ElementName="mycontrol"}" Value="True">
                    <Setter Property="Effect" Value"{StaticResource OrWhatever}"/>
                </DataTrigger>
             </Style.Triggers>
          </Style>
</UserControl>
于 2012-12-18T16:32:44.873 に答える
1

外部に適用できるスタイルでこの効果を設定する必要がある場合は、別のプロパティMyButtonEffectを作成してトリガーで設定できます。

public partial class UserControl1 : UserControl
{
    public Effect MyButtonEffect
    {
        get { return (Effect)GetValue(MyButtonEffectProperty); }
        set { SetValue(MyButtonEffectProperty, value); }
    }

    public static readonly DependencyProperty MyButtonEffectProperty =
        DependencyProperty.Register("MyButtonEffect", typeof(Effect), typeof(UserControl1),
        new FrameworkPropertyMetadata(null, MyButtonEffectPropertyChanged));

    private static void MyButtonEffectPropertyChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        ((UserControl1)obj).myButton.Effect = (Effect)e.NewValue;
    }
}

引き金:

<Trigger Property="IsShown" Value="true">
    <Setter Property="MyButtonEffect" Value="{StaticResource outerGlow}"/>
</Trigger>
于 2012-12-18T16:21:39.497 に答える