2011-07-04 12 views
3

テキストボックスとボタンを持つユーザーコントロールがあります。トリガー(WPF)でプロパティを設定する方法

<UserControl x:Class="MyProject.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" 
     xmlns:l="clr-namespace:MyProject" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
    <UserControl.Resources> 
     <Style TargetType="l:UserControl1" > 
      <Style.Triggers> 
    <Trigger Property="l:UserControl1.IsEditing" Value="True"> 
     <Setter Property="IsEnabled" Value="False"></Setter> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
    </UserControl.Resources> 
    <Grid> 
<Grid.RowDefinitions> 
    <RowDefinition /> 
    <RowDefinition /> 
</Grid.RowDefinitions> 
    <Button Content="Button" HorizontalAlignment="Left" x:Name="button1" VerticalAlignment="Top" Width="75" Grid.Row="0" Click="button1_Click" /> 
    <TextBox Height="23" HorizontalAlignment="Left" x:Name="textBox1" VerticalAlignment="Top" Width="120" Grid.Row="1"/> 
</Grid> 
</UserControl> 

コードは次のとおりです:

using System; 
using System.Windows; 
using System.Windows.Controls; 

    namespace MyProject 
    { 
    /// <summary> 
    /// Interaction logic for UserControl1.xaml 
    /// </summary> 
    public partial class UserControl1 : UserControl 
    { 
     public static readonly DependencyProperty IsEditingProperty = DependencyProperty.Register(
       "IsEditing", typeof(Boolean), typeof(UserControl), new PropertyMetadata(false)); 

     public Boolean IsEditing 
     { 
      get { return (Boolean)GetValue(IsEditingProperty); } 
      set { SetValue(IsEditingProperty, value); } 
     } 

     public UserControl1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      IsEditing = !IsEditing; 
     } 
     } 
} 

は私が

XAMLは以下の通りである(私はコードを経由してこれを行う方法を知っている)トリガを使用して、テキストボックスを無効にしたいです

しかし、この設定はTextBoxとボタンの両方を無効にします。どうすればボタンを無効にすることができますか?私がいくつかのテキストボックスを持っていて、そのうちのいくつかだけを無効にしたい場合は、何が最善のオプションですか? 1つのトリガーを使用して、テキストボックス、カレンダー、データグリッドなどのUIElementsが複数ある場合、どのようにすればいいですか?

答えて

2

はダウンあなたのグリッドにスタイルを移動してみてください、とtextBox1テキストボックスにTargetNameはを設定します。例えば、この質問に対する回答を参照してください:ところでTriggers Based on Properties from DataContext

、あなたが直接textBox1.IsEnabledにIsEditingの値をバインドすることができる必要があります(警告:インプレースコーディング、そのコードがそのまま動作しない場合があります)

<Button IsEnabled="{Binding Path=IsEditing RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl1}}} /> 
+0

私はバインディング技術を惜しんでいます。私はトリガーを使用する方法を見つけることができませんでした。 – mans

+0

バインディングはとにかくきれいだからいいですね。 –

0

SetterTargetNameをボタンの名前に設定します。。

+0

私はこれを行いました:しかし、私はエラーが発生しています:プロパティ 'TargetName' 'textBox1'という名前の要素が見つからなかったためです。ターゲットがそれを使用するセッター、トリガーまたは条件の前に宣言されていることを確認してください。 – mans

0

最良のオプションは、グループ化してあり、その後、一度に無効にし、あなたのコード内の

、あなたはX指定したhaventは:あなたがキーを指定いけない場合、あなたのスタイルのためのキーを、それはとしてそれを使用しようとしますUserControl1のすべてのコントロールの種類の既定のスタイル。次に、そのスタイルをUserControl1の子コントロールに割り当てることができます。

キーの

<Style x:Key="styleDefault" TargetType="Control"> 

は、あなたの子コントロールにスタイルを付ける:

<Button Style="{StaticResource styleDefault}"></Button> 
+0

x:keyをスタイルに追加します。