使用 Blend SDK 事件触发器通过相关列表视图项 MVVM 触发按钮单击

发布于 2024-12-04 17:02:20 字数 2065 浏览 0 评论 0原文

我正在尝试使用 Blend 中的事件触发器来触发列表视图项的按钮单击事件,它应该可以工作,以便为要引用的相关行不必选择该项目。

我的代码是...

Public void MyCommand(object obj)
{
    // the tag of this has the search type
    ListViewItem item = obj as ListViewItem;

    // do my dreary domain work...
}

我的xaml是...

<ListView ItemsSource="{Binding Path=SystemSetupItems}" 
    SelectedItem="{Binding Selected, Mode=TwoWay}" 
    MinHeight="120" >
    <ListView.View>
    <GridView>
        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
        <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}" />
        <GridViewColumn>
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <Button  >
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseClick">
                                <i:InvokeCommandAction CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem, AncestorLevel=1}}" Command="{Binding MyCommand}"/>
                            </i:EventTrigger>
                         </i:Interaction.Triggers>
                     </Button>
                 </DataTemplate>
             </GridViewColumn.CellTemplate>
        </GridViewColumn>                        
    </GridView>
    </ListView.View>
</ListView>

但这根本不起作用,或者我可以在我的xaml按钮定义中执行此操作,

<GridViewColumn.CellTemplate>
    <DataTemplate>
        <Button Command="{Binding OpenWorkSpaceCommand}" CommandParameter="{Binding Path=Name}" Content="Edit..." DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}"  >                                 
        </Button>
    </DataTemplate>
</GridViewColumn.CellTemplate>

但这需要预先选择列表视图项,这不是我想要的行为。

I'm trying to use the event trigger from Blend to fire a button click event of a listview item, it should work so that the item does not have to be selected for the relevant row to be referenced.

My code is...

Public void MyCommand(object obj)
{
    // the tag of this has the search type
    ListViewItem item = obj as ListViewItem;

    // do my dreary domain work...
}

my xaml is...

<ListView ItemsSource="{Binding Path=SystemSetupItems}" 
    SelectedItem="{Binding Selected, Mode=TwoWay}" 
    MinHeight="120" >
    <ListView.View>
    <GridView>
        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
        <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}" />
        <GridViewColumn>
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <Button  >
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseClick">
                                <i:InvokeCommandAction CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem, AncestorLevel=1}}" Command="{Binding MyCommand}"/>
                            </i:EventTrigger>
                         </i:Interaction.Triggers>
                     </Button>
                 </DataTemplate>
             </GridViewColumn.CellTemplate>
        </GridViewColumn>                        
    </GridView>
    </ListView.View>
</ListView>

but this doesn't work at all, alternatively I can do this in my xaml button definition

<GridViewColumn.CellTemplate>
    <DataTemplate>
        <Button Command="{Binding OpenWorkSpaceCommand}" CommandParameter="{Binding Path=Name}" Content="Edit..." DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}"  >                                 
        </Button>
    </DataTemplate>
</GridViewColumn.CellTemplate>

but this required the listview item to be previously selected, which is not the behaviour I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活雷疯 2024-12-11 17:02:20

对于我的 DataGrid,我使用单元格模板在每个项目上都有一个按钮。每个项目都是 Meal 类型的对象。在我的 Meal.cs 文件中,我有一个如下的事件定义:

public Meal()
{
    RemoveMealCommand = new RelayCommand(() => RemoveMealCommandExecute());
}

public RelayCommand RemoveMealCommand
{
    get;
    set;
}

public delegate void RemoveMealEventHandler(object sender, EventArgs e);
public event RemoveMealEventHandler RemoveMealEvent;

private void RemoveMealCommandExecute()
{
    RemoveMealEvent(this, null);
}

在列表中每顿饭的视图模型中,我只需向该事件添加一个处理程序。对于我的 xaml 按钮,我只需将命令设置为 Meal 的 RelayCommand。

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Command="{Binding Path=RemoveMealCommand}">
                <Image Width="13" Height="13" Source="/Images/delete-icon.png"/>
            </Button>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

现在,当您单击按钮时,Meal 负责触发事件,而 viewmodel 负责处理它。

For my DataGrid I have a button on each item using the cell template. Each item is an object of type Meal. In my Meal.cs file I have an event definition like so:

public Meal()
{
    RemoveMealCommand = new RelayCommand(() => RemoveMealCommandExecute());
}

public RelayCommand RemoveMealCommand
{
    get;
    set;
}

public delegate void RemoveMealEventHandler(object sender, EventArgs e);
public event RemoveMealEventHandler RemoveMealEvent;

private void RemoveMealCommandExecute()
{
    RemoveMealEvent(this, null);
}

In my viewmodel for every meal in my list I can just add a handler to that event. And for my xaml button I just set the command to the Meal's RelayCommand.

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Command="{Binding Path=RemoveMealCommand}">
                <Image Width="13" Height="13" Source="/Images/delete-icon.png"/>
            </Button>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Now when you click the button the Meal is responsible for firing the event and the viewmodel is responsible for handling it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文