2012-12-18 69 views
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(); 
    } 
} 

我想添加效果myButton的時候被示爲真。在主窗口,

<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。任何人都可以告訴我屬性路徑屬於哪個類別?

回答

1

你不能做到這一點。

如果需要影響控制這是一個用戶控件的範圍內,你將需要定義用戶控件的屬性和參考,在使用的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> 
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>