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

当您需要在可以外部应用的样式中设置此效果时,您可以创建另一个属性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 回答
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 回答