编写属性路径以查找控件的属性

时间:2012-12-18 16:19:12

标签: c# wpf user-controls

我有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为真时为myButton添加效果。在MainWindow中,

<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无法解决符号myButton

如何解决?

更新

我找到了the MSDN article。谁能告诉我属性路径属于哪个类别?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>