在ControlTemplate中设置CommandBinding吗? [英] Set CommandBinding in ControlTemplate?!

查看:85
本文介绍了在ControlTemplate中设置CommandBinding吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...

我有许多具有相同结构的ListView,所以确实为此创建了一个Datatemplate.

Hey Guys...

I have many ListViews with same structure, so I did create a Datatemplate for this.

<ControlTemplate x:Key="personalGridViewTmpl">
            <ListView Name="lvBlah" ctrl:ListViewLayoutManager.Enabled="True" ItemsSource="{Binding}" MouseDoubleClick="ListBox_MouseDoubleClick">
                <ListView.View>
                    <GridView>
                        <GridViewColumn ctrl:ProportionalColumn.Width="1" DisplayMemberBinding="{Binding ID}">
                            <GridViewColumnHeader Content="ID" Command="{x:Static w:MyCommands.doubleClick}"/>
                        </GridViewColumn>
                        <GridViewColumn ctrl:ProportionalColumn.Width="2" DisplayMemberBinding="{Binding Nachname}">
                            <GridViewColumnHeader Content="Nachname" Command="{x:Static w:MyCommands.doubleClick}"/>
                        </GridViewColumn>
                        <GridViewColumn ctrl:ProportionalColumn.Width="2" DisplayMemberBinding="{Binding Vorname}">
                            <GridViewColumnHeader Content="Vorname" Command="{x:Static w:MyCommands.doubleClick}"/>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </ControlTemplate>



在那里我称之为ControlTemplate



There I do call this ControlTemplate

<ListView Name="lvSchichtAlle" DataContext="{Binding AngestellteSchicht}" Template="{StaticResource personalGridViewTmpl}"/>



我有问题,我无法在此模板的ListView上设置CommandBinding.在Listview"lvSchichtAlle"上设置CommandBinding时,没有内容,因为它在TemplateListView中

lg



I have the problem, that I can''t set a CommandBinding on the ListView of this Template. When the CommandBinding is set on Listview "lvSchichtAlle" there is no content, becaus it''s in the TemplateListView

lg

推荐答案

命令绑定可以在窗口级别设置.大多数命令是在WPF中的元素树上路由的,因此引发的命令(尽管在模板中)实际上遵循Listview"lvSchichtAlle"的元素树.这意味着跟随是合法的:


The command binding can be set at the window level. Most commands are routed up the element tree in WPF so the command raised (though in the template) actucally follows the element tree of the Listview "lvSchichtAlle". This means the follwing is legitimate:


<Window x:Class="WpfApplication1.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300">
    <Window.CommandBindings>
        <CommandBinding

            Command="{x:Static w:MyCommands.doubleClick}"

            CanExecute="CommandBinding_CanExecute"

            Executed="CommandBinding_Executed" />

    </Window.CommandBindings>

    <Window.Resources>
        <ResourceDictionary>
            <ControlTemplate x:Key="personalGridViewTmpl">
                <ListView Name="lvBlah" ItemsSource="{Binding}" MouseDoubleClick="ListBox_MouseDoubleClick">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn DisplayMemberBinding="{Binding ID}">
                                <GridViewColumnHeader Content="ID" Command="{x:Static w:MyCommands.doubleClick}"/>
                            </GridViewColumn>
                            <GridViewColumn  DisplayMemberBinding="{Binding Nachname}">
                                <GridViewColumnHeader Content="Nachname" Command="{x:Static w:MyCommands.doubleClick}"/>
                            </GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Vorname}">
                                <GridViewColumnHeader Content="Vorname" Command="{x:Static w:MyCommands.doubleClick}"/>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>
            </ControlTemplate>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <ListView Name="lvSchichtAlle" DataContext="{Binding AngestellteSchicht}" Template="{StaticResource personalGridViewTmpl}" />
    </Grid>
</Window>


这篇关于在ControlTemplate中设置CommandBinding吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆