绑定IsEnabled的属性不起作用了。
XAML代码如下
Code
C#代码如下
public class MainPageViewModel : INotifyPropertyChanged
{
public MainPageViewModel()
{
BTNEnabledCommand = new DelegateCommand<object>(OnEnabledCommand);
BTNClickCommand = new DelegateCommand<object>(OnClickCommand);
}
private void OnEnabledCommand(object arg)
{
IsNewEnabled = !IsNewEnabled;
}
private void OnClickCommand(object arg)
{
MessageBox.Show("Hello Wingfay");
}
public ICommand BTNClickCommand { get; private set; }
public ICommand BTNEnabledCommand { get; private set; }
private bool _IsNewEnabled;
public bool IsNewEnabled
{
get
{
return _IsNewEnabled;
}
set
{
_IsNewEnabled = value;
OnPropertyChanged("IsNewEnabled");
}
}
....
}
郁闷死我了,在网上找资料发现了这个 {
public MainPageViewModel()
{
BTNEnabledCommand = new DelegateCommand<object>(OnEnabledCommand);
BTNClickCommand = new DelegateCommand<object>(OnClickCommand);
}
private void OnEnabledCommand(object arg)
{
IsNewEnabled = !IsNewEnabled;
}
private void OnClickCommand(object arg)
{
MessageBox.Show("Hello Wingfay");
}
public ICommand BTNClickCommand { get; private set; }
public ICommand BTNEnabledCommand { get; private set; }
private bool _IsNewEnabled;
public bool IsNewEnabled
{
get
{
return _IsNewEnabled;
}
set
{
_IsNewEnabled = value;
OnPropertyChanged("IsNewEnabled");
}
}
....
}
以上是Prism开发人员的回答
然后看了下Prism关于Command的源代码 终于明白了点
该后的代码如下
XAML
<UserControl.Resources>
<Local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<Local:MainPageViewModel x:Key="MainPageViewModelDataSource" d:IsDataSource="True"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MainPageViewModelDataSource}}">
<Button Height="36" HorizontalAlignment="Left" Margin="128,126,0,0" VerticalAlignment="Top" Width="58" Content="New"
Visibility="{Binding IsNewVisible, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}"
commands:Click.Command="{Binding BTNClickCommand}"
/>
<Button Height="36" Margin="292,126,273,0" VerticalAlignment="Top" Content="Enabled" commands:Click.Command="{Binding BTNEnabledCommand}"/>
</Grid>
<Local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<Local:MainPageViewModel x:Key="MainPageViewModelDataSource" d:IsDataSource="True"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MainPageViewModelDataSource}}">
<Button Height="36" HorizontalAlignment="Left" Margin="128,126,0,0" VerticalAlignment="Top" Width="58" Content="New"
Visibility="{Binding IsNewVisible, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}"
commands:Click.Command="{Binding BTNClickCommand}"
/>
<Button Height="36" Margin="292,126,273,0" VerticalAlignment="Top" Content="Enabled" commands:Click.Command="{Binding BTNEnabledCommand}"/>
</Grid>
C#代码
Code
*转载请注明来自哪里就行。