2009-06-30 20 views
0

我的意思是這個。爲了測試我需要當用戶檢CHK1CHK2元素更改屬性的IsEnabled但我不能這樣做參考CHK2元素以風格調用WPF樹中的其他元素

這是款式XAML。

<Style x:Key="styleCheckBox" TargetType="{x:Type CheckBox}"> 
      <Style.Triggers> 
       <Trigger Property="IsChecked" Value="True"> 

      </Style.Triggers> 
</Style 

電話樣式..

<StackPanel> 
     <CheckBox x:Name="chk1" Content="CheckBox1" Style="{StaticResource styleCheckBox}"/> 
     <CheckBox x:Name="chk2" Content="CheckBox2"/> 
    </StackPanel> 

回答

3

您不能在樣式觸發設置TargetProperty。這基本上意味着你應該創建一個派生自StackPanel的自定義控件,其中包含兩個複選框,並且這些複選框顯示爲屬性。然後你就可以爲該控件定義一個樣式(不是CheckBox)並設置你想要的屬性。

更簡單的方法(如果只需要測試)會是這樣:

<StackPanel> 
<StackPanel.Resources> 
    <local:InverseBoolConverter x:Key="InverseBoolConverter"/> 
</StackPanel.Resources> 
<CheckBox x:Name="chk1" Content="CheckBox1"/> 
<CheckBox x:Name="chk2" Content="CheckBox2" IsEnabled="{Binding ElementName=chk1, Path=IsChecked, Converter={StaticResource InverseBoolConverter}}"/> 
</StackPanel> 

凡InverseBoolConverter定義如下:

[ValueConversion(typeof(bool), typeof(bool))] 
public class InverseBoolConverter: IValueConverter { 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { 
     if(value is bool) 
      return !(bool)value; 
     else 
      return null; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { 
     if(value is bool) 
      return !(bool)value; 
     else 
      return null; 
    } 
} 
+0

謝謝!,是非常有用的。 – Rangel 2009-06-30 08:44:19