user746461
user746461

Reputation:

Write a Property Path to find a property of a control

I have a 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>

and code-behind:

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();
    }
}

I want to add an Effect to myButton when IsShown is true. In the 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>

But VS cannot solve symbol myButton.

How to fix it?

Update:

I found the MSDN article. Can anyone tell me which category my property path falls in?

Upvotes: 0

Views: 211

Answers (2)

Clemens
Clemens

Reputation: 128136

When you need to set this effect in a Style that can be applied externally, you could create another property MyButtonEffect and set that in the Trigger.

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;
    }
}

The Trigger:

<Trigger Property="IsShown" Value="true">
    <Setter Property="MyButtonEffect" Value="{StaticResource outerGlow}"/>
</Trigger>

Upvotes: 0

Fede
Fede

Reputation: 44048

You can't do that.

If you need to affect a control which is inside the scope of a UserControl, you will need to define a property IN the UserControl and reference that in your inner control using RelativeSource or 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>

Upvotes: 0

Related Questions