2009-08-26 10 views
2

텍스트 상자의 텍스트 값을 기반으로 단추를 비활성화하고 활성화해야하는 시나리오가 있습니다. TextBox.Text = "abc"또는" cdf "버튼을 비활성화하고 다른 값을 활성화해야합니다.wpf의 다른 컨트롤의 속성을 기반으로 컨트롤의 속성을 설정하는 방법

이것은 Xaml에서만 작성되어야합니다. 이 할 수없는 XAML에 엄격해야하고, 나 같은 요구 사항을 수행 사전

+0

은 XAML에 기록 될? –

답변

2

에서

덕분에 의미를. 당신의 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 이외의 값으로 변경되면 활성화됩니다. 이에 대한 요구 사항을 가지고 왜

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

관련 문제