【发布时间】:2021-09-14 18:03:25
【问题描述】:
我有一个用户控件列表,每个用户控件都有两个按钮,当我点击它们时,一定会发生一些事情,但我想不在用户控件内部处理这个事件,我想在主页 那么,如何捕获列表视图的选定项用户控件触发的事件?
后面的用户控制代码:
public sealed partial class TestingUerControl : UserControl
{
public TestingUerControl()
{
this.InitializeComponent();
}
public event EventHandler FirstButtonEvent;
public event EventHandler SecondButtonEvent;
private void firstButton_Click(object sender, RoutedEventArgs e)
{
//Some stuff of code
FirstButtonEvent?.Invoke(this, new EventArgs());
}
private void secondButton_Click(object sender, RoutedEventArgs e)
{
//Some stuff of code
SecondButtonEvent?.Invoke(this, new EventArgs());
}
}
主页 xaml 标记:
<ListView x:Name="listUserControl"
Width="100"
Header="400">
<ListView.ItemTemplate>
<DataTemplate x:DataType="model:MyModel">
<userControl:TestingUerControl/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我用了这个说法:
((TestingUerControl)listUserControl.SelectedItem).FirstButtonEvent += OnFirstButtonEvent;
但这不起作用我只能将 SelectedItem 转换为 MyModel 类 那么如何才能到达列表视图的选定用户控件的“FirstButtonEvent”和“SecondButtonEvent”
【问题讨论】:
-
控件应该公开两个绑定到 MyModel 类的 ICommand 属性的 ICommand 属性。
-
我想在不使用 mvvm 的情况下做到这一点,如果你给我看一些代码,我将不胜感激