사용자가 뭔가를 TextBox
에 입력 할 때 Button
을 사용하는 가장 간단한 방법은 WPF에서 무엇입니까?사용자가 텍스트 상자에 입력 할 때 단추를 활성화하는 방법
17
A
답변
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하는 일반적인 지능을 사용하여 예를 들어
,
<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>
아무것도 다른 필요 ......이의 키가이는 UpdateSourceTrigger =하여 PropertyChanged
추가 자체를 바인딩 ..
에
0
가장 간단한 해결책
+1 단순하고 작동합니다 - 감사합니다! –
U, ser, 천재 야! –
소원 나는 이것을 더 upvote 수 – cppguy