2009-05-04 20 views

答えて

0

使用シンプルなコマンドここで

<TextBox Text={Binding Path=TitleText}/> 

<Button Command="{Binding Path=ClearTextCommand}" Content="Clear Text"/> 

はマーロンGrechsを参照してください詳細についてはビューモデル

public class MyViewModel : INotifyPropertyChanged 
{ 
    public ICommand ClearTextCommand { get; private set; } 

    private string _titleText; 
    public string TitleText 
    { 
     get { return _titleText; } 
     set 
     { 
      if (value == _titleText) 
       return; 

      _titleText = value; 
      this.OnPropertyChanged("TitleText"); 
     } 
    } 

    public MyViewModel() 
    { 
     ClearTextCommand = new SimpleCommand 
      { 
       ExecuteDelegate = x => TitleText="", 
       CanExecuteDelegate = x => TitleText.Length > 0 
      }; 
    }    

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = this.PropertyChanged; 
     if (handler != null) 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
    }  
} 

のサンプルコードでSimpleCommand

またMVVMプロジェクトテンプレートをチェックアウト/ツールキットはhttp://blogs.msdn.com/llobo/archive/2009/05/01/download-m-v-vm-project-template-toolkit.aspxです。これはDelegateCommandを使用してコマンドを実行します。これは、どのプロジェクトでも優れた開始テンプレートになるはずです。

-1

ストロークごとに発生するTextBoxにコールバックを追加します。そのようなコールバックで空をテストし、ボタンを有効/無効にします。

2

コマンドを使用していなかった場合は、コンバータを使用する方法もあります。ボタンISENABLEDプロパティに続い

[ValueConversion(typeof(int), typeof(bool))] 
    public class IntToBoolConverter : IValueConverter 
    { 
    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     try 
     { 
     return (System.Convert.ToInt32(value) > 0); 
     } 
     catch (InvalidCastException) 
     { 
     return DependencyProperty.UnsetValue; 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return System.Convert.ToBoolean(value) ? 1 : 0; 
    } 

    #endregion 
    } 

:コンバータBOOLする汎用のIntを用いて例えば

<Button IsEnabled={Binding ElementName=TextBoxName, Path=Text.Length, Converter={StaticResource IntToBoolConverter}}/> 

HTHを

デニス

2

使用トリガー!

<TextBox x:Name="txt_Titel /> 
<Button Content="Transfer" d:IsLocked="True"> 
    <Button.Style> 
    <Style> 
     <Style.Triggers> 
     <DataTrigger Binding="{Binding ElementName=txt_Titel, Path=Text}" Value=""> 
     <Setter Property="Button.IsEnabled" Value="false"/> 
     </DataTrigger> 
     </Style.Triggers> 
    </Style> 
    </Button.Style> 
</Button> 
103

誰もが物事を複雑にしているのはなぜですか?

<TextBox x:Name="TB"/> 
    <Button IsEnabled="{Binding ElementName=TB,Path=Text.Length}">Test</Button> 

必要な他には何も......

+4

+1シンプルで働く - ありがとう! –

+0

U、ser、は天才です! –

+0

私はこれ以上upvoteできますか – cppguy

0

この上のキー自体を結合..

であるが、これはあるUpdateSourceTrigger =のPropertyChanged

を追加します。最も単純なソリューション

関連する問題