C# 如何在ListView中未选择任何项目时禁用按钮
我有一个C# 如何在ListView中未选择任何项目时禁用按钮,c#,wpf,xaml,listview,user-controls,C#,Wpf,Xaml,Listview,User Controls,我有一个ListView包含在一个UserControl中。如果在UserControl中没有选择任何项目,我想禁用一个按钮,这样做是否正确?到目前为止,它没有禁用,只是一直保持启用状态。 我已经包含了xaml代码 searchAccountUserControl是xaml中的UserControl名称属性。 AccountListView是userControl xaml中的ListViewname属性 <Button Content="Debit" IsEnabled="true" H
ListView
包含在一个UserControl中。如果在UserControl中没有选择任何项目,我想禁用一个按钮,这样做是否正确?到目前为止,它没有禁用,只是一直保持启用状态。
我已经包含了xaml代码
searchAccountUserControl是xaml中的UserControl名称属性。
AccountListView是userControl xaml中的ListView
name属性
<Button Content="Debit" IsEnabled="true" HorizontalAlignment="Left" Margin="18,175,0,0" Name="DebitButton" Width="128" Grid.Column="1" Height="32" VerticalAlignment="Top" Click="DebitButton_Click">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=searchAccountUserControl.AccountListView, Path=SelectedValue}" Value="{x:Null}" >
<Setter Property="Button.IsEnabled" Value="false"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
然后在xaml中使用isEnabled=“{Binding Path=IsSelected}”。这里有一些错误
IsEnabled
,则样式将永远无法更改它ElementName
,不是一个路径,只是一个给出一个元素名称的字符串。除此之外的所有东西都会进入系统Style.TargetType
,则不应使用类型前缀设置Setter.Property
(尽管保留它不会破坏Setter)<Button IsEnabled="{Binding SelectedItems.Count, ElementName=lv}" ...
很明显,您没有使用命令()。您应该使用它(最好是模型视图视图模型体系结构)
但是,如果您想坚持使用代码隐藏和XAML:
<ListView SelectionChanged="AccountListView_SelectionChanged" ... />
private void AccountListView_SelectionChanged(Object sender, SelectionChangedEventArgs args)
{
DebitButton.IsEnabled = (sender != null);
//etc ...
}
优点。。。我完全忽略了他只使用xaml的方法,使用了MVVM讲座+xaml/代码隐藏混合(他已经在使用这种混合)。我的xaml使用baseWindowViewModel,我也使用绑定路径。但正如我所说,我不知道如何使用绑定,所以我试图找到一种处理代码隐藏的方法,尽管我更愿意使用ViewModel方法来保持它的直截了当。所以我想我可以使用并制作一个转换器?@Jonathan:如果你观察优先级并修复绑定,触发器也会起作用,但是,如果您可以直接绑定到ListView,您可以按照上面所示进行绑定,这将导致将计数转换为bool:0 selected->false=disabled;1个或多个selected->true=enabled@Hb:由于listView位于userControl中,我不知道是否可以直接绑定它,我不知道如我所说,保持模式并保持代码干净的最佳方法是什么。我是否可以像实际使用模型-视图-模型体系结构那样进行操作。我只是不知道如何做到这一点,并在互联网上寻找线索。你正在使用什么框架(如果有的话)?使用事件不是MVVM。。。绑定(包括命令绑定)是MVVM。是的,我知道我在谈论代码的其他部分。使用viewModel和{Binding Path=…Converter ect…}@Jonathan:我假设您没有使用MVVM的原因是您命名了XAML控件(不必要),并且您的按钮上有一个单击事件(不必要)。好的,是的,我们所有的viewModels都实现了INotifyPropertyChanged,我想我们只是没有使用RelayCommand。MVVM不仅仅是使用绑定。它将视图从ViewModel中分离出来。您的解决方案(虽然可能有效)将视图与ViewModel耦合。但是,嘿,如果它起作用,它就起作用了?
<ListView SelectionChanged="AccountListView_SelectionChanged" ... />
private void AccountListView_SelectionChanged(Object sender, SelectionChangedEventArgs args)
{
DebitButton.IsEnabled = (sender != null);
//etc ...
}
public class MyViewModel : ViewModelBase //Implements INotifyPropertyChanged
{
public MyViewModel()
{
DoSomethingCommand = new RelayCommand(DoSomething, CanDoSomething);
}
public ObservableCollection<Object> MyItems { get; set; }
public Object SelectedItem { get; set; }
public RelayCommand DoSomethingCommand { get; set; }
public void DoSomething() { }
public Boolean CanDoSomething() { return (SelectedItem != null); }
}
<ListView ItemsSource="{Binding MyItems}" SelectedItem="{Binding SelectedItem}" ... />
<Button Command="{Binding DoSomethingCommand}" ... />