2009-08-26 15 views
2

テキストボックスのテキスト値に基づいて、ボタンを無効にして有効にしなければならないシナリオがあります。TextBox.Text = "abc"または" cdf "ボタンを無効にし、他の値を有効にする必要があります。wpfの他のコントロールのプロパティに基づいてコントロールのプロパティを設定する方法

これはXamlでのみ記述する必要があります。アドバンス

+0

? –

答えて

2

おかげでこれはXAMLで厳密に行うことは不可能である、とも、そのような要件は意味をなさないありません。あなたのXAMLで、その後

public class MyViewModel : ViewModel 
{ 
    private string _text; 

    public string Text 
    { 
     get { return _text; } 
     set 
     { 
      if (_text != value) 
      { 
       _text = value; 
       OnPropertyChanged("Text"); 
       OnPropertyChanged("IsButtonEnabled"); 
      } 
     } 
    } 

    public bool IsButtonEnabled 
    { 
     get { return _text != "abc"; } 
    } 
} 

:これは、ビューモデルで明らかにされなければならないビジネスロジックである

ボタンが無効になります:あなたがこれを行うためにトリガを使用することができますように

<TextBox Text="{Binding Text}"/> 
<Button IsEnabled="{Binding IsButtonEnabled}"/> 
+1

この質問に対する別の回答のように、トリガーで行うこともできますが、代わりにViewModelで行う必要があります。 – LJNielsenDk

7

が見えます値ABCがテキストボックスに入力された後、値がABC以外の値に変更されると有効になります。あなたが唯一のXAMLで記述するために、このための要件を持っているのはなぜ

<Window x:Class="WpfApplication5.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="300" Width="300"> 

<Window.Resources> 
    <Style x:Key="disableButton" TargetType="{x:Type Button}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding ElementName=textBox1,Path=Text}" Value="ABC"> 
       <Setter Property="IsEnabled" Value="False" /> 
      </DataTrigger> 

     </Style.Triggers> 
    </Style> 
</Window.Resources> 

<StackPanel> 
    <TextBox x:Name="textBox1"/> 
    <Button Style="{StaticResource disableButton}" Height="23" Name="button1" Width="75">Button</Button> 
</StackPanel> 

関連する問題