Iva*_*Iva 5 c# wpf xaml frontend mvvm
这是我的Button声明,用.xaml文件编写:
<dxlc:LayoutGroup Orientation="Horizontal" Margin="5,15,0,5">
<Grid MinWidth="100">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button
IsEnabled="{Binding IsSearchCriteriaHasValue}"
Content="Search"
MaxHeight="25"
MaxWidth="70"
ClipToBounds="True"
VerticalAlignment="Center"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Command="{Binding SearchCommand}"/>
</Grid>
</dxlc:LayoutGroup>
Run Code Online (Sandbox Code Playgroud)
这是返回true/false的函数,无论用户是否在搜索按钮旁边的搜索框中键入了任何搜索文本.该函数位于另一个.cs文件中:
public bool isButtonEnabled
{
return (SearchBox.Selection.Count > 0);
}
Run Code Online (Sandbox Code Playgroud)
问题是isEnabled的值永远不会改变,它保持为真,即按钮始终保持启用状态,或者如果我更改>符号,按钮始终处于禁用状态.有什么建议?
该IsSearchCriteriaHasValue需求引发一个事件,它改变了,你可以通过使用INotifyPropertyChanged界面:
public class Customer : INotifyPropertyChanged
{
// INotifyPropertyChanged members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
// Your property
private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged(new PropertyChangedEventArgs("Name"));
}
}
}
Run Code Online (Sandbox Code Playgroud)