如何为wpf中不可能的控件设置命令绑定?例如如何在listBox项目中设置命令?或者如何在treeView项目中设置命令?
<ListBox Style="{StaticResource MaterialDesignToolToggleListBox}" ItemContainerStyle="{DynamicResource _ListBoxItemStyle}" >
<ListBoxItem >
<materialDesign:PackIcon VerticalAlignment="Center" HorizontalAlignment="Center" Kind="MicrosoftWindows" />
</ListBoxItem>
<ListBoxItem>
<materialDesign:PackIcon Kind="Games" />
</ListBoxItem>
<ListBoxItem IsSelected="True">
<materialDesign:PackIcon Kind="Video" />
</ListBoxItem>
<ListBoxItem>
<materialDesign:PackIcon Kind="Image" />
</ListBoxItem>
</ListBox>发布于 2019-08-26 16:58:19
ListBoxItem没有实现ICommandSource。这就是它不能执行ICommand的原因。要解决此问题,您可以覆盖ListBoxItem的模板,并使用Button作为内容宿主:
MainWindow.xaml
<Window>
<Window.DataContext>
<ViewModel />
</Window.DataContext>
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Button Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
Content="{TemplateBinding Content}"
Command="{Binding MyCommand}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem>
<materialDesign:PackIcon VerticalAlignment="Center"
HorizontalAlignment="Center"
Kind="MicrosoftWindows" />
</ListBox>
</Window>如果需要为每个项分配单独的命令,则需要定义一个带有Button的ListBox.ItemTemplate作为内容主机,它将Button.Command绑定到数据项的ICommand属性。
ViewModel.cs
class LabelData : INotifyPropertyChanged
{
private string label;
public string Label
{
get => this.label;
set
{
this.label = value;
OnPropertyChanged();
}
}
private ICommand myCommand;
public ICommand MyCommand
{
get => this.myCommand;
set
{
this.myCommand = value;
OnPropertyChanged();
}
}
// Constructor
// Initialize the data binding source of the ListBoxItems
public void LabelData(string label)
{
this.Label = label;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}ViewModel.cs
class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<LabelData> labels;
public ObservableCollection<LabelData> Labels
{
get => this.labels;
set
{
this.labels = value;
OnPropertyChanged();
}
}
// Constructor
// Initialize the data binding source of the ListBox
public void ViewModel()
{
this.Labels = new ObservableCollection<LabelData>()
{
new LabelData("MicrosoftWindows") { MyCommand = SomeCommand},
new LabelData("Games") { MyCommand = SomeOtherCommand},
new LabelData("Video") { MyCommand = SomeOtherCommand},
new LabelData("Image") { MyCommand = SomeCommand}
};
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}MainWindow.xaml
<Window>
<Window.DataContext>
<ViewModel />
</Window.DataContext>
<ListBox ItemsSource="{Binding Labels}">
<ListBox.ItemTemplate>
<DataTemplate DataType="LabelData">
<Button Command="{Binding MyCommand}">
<Button.Content>
<materialDesign:PackIcon Kind="{Binding Label}" />
</Button.Content>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Window>这两个选项也适用于TreeView。
第三种选择是使用Attached属性将ICommand附加到每个ListBoxItem或任何其他Control (基本上附加到任何DependencyObject)。
第四种选择是通过扩展ListBoxItem和实现ICommandSource来创建自定义项。
发布于 2019-08-26 14:02:37
你到底是什么意思?Command属性只是Button_Click属性的“绑定方式”。如果你想有一个ListBoxItem,你可以点击并调用一个方法,那么只需使用下面的代码:
<Grid>
<ListBox>
<ListBoxItem>
<Button Content="Test"
Command="{Binding}">
</Button>
</ListBoxItem>
</ListBox>
</Grid>https://stackoverflow.com/questions/57652423
复制相似问题