C# 如何将命令绑定添加到动态按钮?
经过广泛的研究,我还没有找到这个问题的答案。我有一个列表框,其ItemsSource是按钮对象的集合。当我向集合中添加一个按钮时,它会正确显示,但当单击时,该命令不会执行。我已经实现了RelayCommand,并且在我的代码中都使用了它 C#MVVM WPF 景色C# 如何将命令绑定添加到动态按钮?,c#,button,mvvm,command,C#,Button,Mvvm,Command,经过广泛的研究,我还没有找到这个问题的答案。我有一个列表框,其ItemsSource是按钮对象的集合。当我向集合中添加一个按钮时,它会正确显示,但当单击时,该命令不会执行。我已经实现了RelayCommand,并且在我的代码中都使用了它 C#MVVM WPF 景色 <ListBox ItemsSource="{Binding Buttons}" HorizontalAlignment="Stretch"
<ListBox ItemsSource="{Binding Buttons}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Margin="5,5,5,5"
Content="{Binding Content}"
Command="{Binding ExecuteButtonCommand}"
CommandParameter="{Binding CommandParameter}"
/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox ItemsSource="{Binding Buttons, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="AliceBlue"
BorderBrush="Transparent"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectedItem="">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Button Margin="5,5,5,5"
Content="{Binding Content}"
Command="{Binding Command}"
CommandParameter="{Binding CommandParameter}"
/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
为了测试,我有这个代码
public void AddButtons()
{
Buttons= new ObservableCollection<Button>();
Button btn = new Button();
btn.Content = "Generate Files";
btn.Command = "{Binding ExecuteButtonCommand}";
btn.CommandParameter = "Files";
Buttons.Add(btn);
}
public void AddButtons()
{
按钮=新的ObservableCollection();
按钮btn=新按钮();
btn.Content=“生成文件”;
btn.Command=“{Binding ExecuteButtonCommand}”;
btn.CommandParameter=“文件”;
按钮。添加(btn);
}
但我不能这样分配命令。按钮的其余部分工作正常。因此,我将命令=放在您上面看到的视图中
如果这个问题已经得到解答,那么我就找不到了。最接近的答案是9岁,而且不起作用
感谢您的查看。发生的情况是,ListBox的数据模板正在尝试绑定到一个名为ExecuteButtonCommand的属性,该属性在Button对象中不存在。然后,要绑定参数,需要指向视图的DataContext 将其更改为:
<ListBox.ItemTemplate>
<DataTemplate>
<Button Margin="5,5,5,5"
Content="{Binding Content}"
Command="{Binding Command}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext.MyParameter}"
/>
</DataTemplate>
</ListBox.ItemTemplate>
我想以最后的结果结束这篇文章,以防其他人正在寻找相同的答案。 Mari让我明白了这一点,最终得出了下面这个例子。没有“代码隐藏”。按钮的生成是在视图模型中完成的。创建按钮后,它将添加到按钮集合中,按钮集合是列表框的源。我只包括问题的特定代码 结果就是这样 景色
<ListBox ItemsSource="{Binding Buttons}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Margin="5,5,5,5"
Content="{Binding Content}"
Command="{Binding ExecuteButtonCommand}"
CommandParameter="{Binding CommandParameter}"
/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox ItemsSource="{Binding Buttons, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="AliceBlue"
BorderBrush="Transparent"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectedItem="">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Button Margin="5,5,5,5"
Content="{Binding Content}"
Command="{Binding Command}"
CommandParameter="{Binding CommandParameter}"
/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我希望这能帮助别人。太好了,玛丽!由于我尝试了各种不同的方法,我的问题的参数部分被搞砸了。我将btn.CommandParameter=放回按钮创建中。它工作得很好。现在我可以继续添加其余的按钮。我想我已经接近解决办法了。但一切都有点不对劲。现在我明白了。再次感谢你,很好!很高兴这有帮助!:)
<ListBox ItemsSource="{Binding Buttons, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="AliceBlue"
BorderBrush="Transparent"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectedItem="">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Button Margin="5,5,5,5"
Content="{Binding Content}"
Command="{Binding Command}"
CommandParameter="{Binding CommandParameter}"
/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public void AddButton(string param)
{
Button btn = new Button();
switch (param)
{
case "Files":
btn.Content = "Do Files";
btn.CommandParameter = "Files";
btn.Name = "Files";
break;
//More items here
}
btn.Command = ExecuteButtonCommand; //The ICommand name. I made this harder than it needed to be!
Buttons.Add(btn);
}
public RelayCommand _executeButtonCommand;
public ICommand ExecuteButtonCommand
{
get
{
if (_executeButtonCommand == null)
_executeButtonCommand = new RelayCommand(param => this.ButtonCommands(param));
return _executeButtonCommand;
}
}