结合内DataGridTemplateColumn命令 [英] Binding Command inside DataGridTemplateColumn
问题描述
我有视图(XAML附带)贴在我的ViewModel与命令。我需要调用命令时DataGrid的行按钮点击。我使用的行为,这(常规指挥有同样的问题)。当我点击按钮的DataGrid - 我的命令不被解雇
要说明的问题 - 我把列表框的底部有相同的约束力的东西 - 是的,指挥的作品。所以,这是一些与数据网格/ DataGridTemplateColumn
<电网X:NAME =LayoutRoot背景=白的DataContext ={结合}>
< Grid.RowDefinitions>
&所述; RowDefinition高度=30/>
< RowDefinition高度=*/>
< RowDefinition高度=*/>
< /Grid.RowDefinitions>
< StackPanel的方向=横向>
<按钮内容=取消>
< I:Interaction.Triggers>
< I:的EventTrigger事件名称=点击>
< EI:CallMethodAction方法名=取消TargetObject ={结合}/>
< /我:&的EventTrigger GT;
< /我:Interaction.Triggers>
< /按钮>
< / StackPanel的>
< SDK:数据网格的AutoGenerateColumns =FALSEIsReadOnly =真的ItemsSource ={绑定数据}Grid.Row =1>
< SDK:DataGrid.Columns>
< SDK:DataGridTemplateColumn CanUserReorder =真CanUserResize =真CanUserSort =真WIDTH =自动>
< SDK:DataGridTemplateColumn.CellTemplate>
<&DataTemplate的GT;
<按钮内容=选择>
< I:Interaction.Triggers>
< I:的EventTrigger事件名称=点击>
< I:InvokeCommandAction命令={绑定的ElementName =控制,路径= DataContext.ItemSelectedCommand}CommandParameter ={结合}/>
< /我:&的EventTrigger GT;
< /我:Interaction.Triggers>
< /按钮>
< / DataTemplate中>
< / SDK:DataGridTemplateColumn.CellTemplate>
< / SDK:DataGridTemplateColumn>
< SDK:DataGridTextColumn绑定={结合DEVICEID}CanUserReorder =真CanUserResize =真CanUserSort =真标题=设备WIDTH =自动粗细=大胆/>
< SDK:DataGridTextColumn绑定={结合的SerialNumber}CanUserReorder =真CanUserResize =真CanUserSort =真标题=序列号WIDTH =自动/>
< SDK:DataGridTextColumn绑定={结合LastActivityOn}CanUserReorder =真CanUserResize =真CanUserSort =真标题=最近活动WIDTH =自动/>
< SDK:DataGridTextColumn绑定={结合客户机版本}CanUserReorder =真CanUserResize =真CanUserSort =真标题=客户端版本WIDTH =自动/>
< SDK:DataGridTextColumn绑定={结合OSVERSION}CanUserReorder =真CanUserResize =真CanUserSort =真标题=操作系统版本WIDTH =自动/>
< / SDK:DataGrid.Columns> < / SDK:DataGrid的>
<列表框Grid.Row =2的ItemsSource ={绑定数据}>
< ListBox.ItemTemplate>
<&DataTemplate的GT;
< StackPanel的方向=横向>
< TextBlock的文本={结合DEVICEID}>< / TextBlock的>
<按钮内容=选择>
< I:Interaction.Triggers>
< I:的EventTrigger事件名称=点击>
< I:InvokeCommandAction命令={绑定的ElementName =控制,路径= DataContext.ItemSelectedCommand}CommandParameter ={结合}/>
< /我:&的EventTrigger GT;
< /我:Interaction.Triggers>
< /按钮>
< / StackPanel的>
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>
< /网格和GT;
我是pretty确保在的DataGrid
,你仍然需要在<一个href=\"http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx\">DataContextProxy使绑定工作。本的ElementName绑定不起作用(是的,它正在为的ListBox
模板,但不是的DataGrid
,这是因为DataGridTemplateColumn是不是在视觉树),即使在Silverlight 4中。
更新
在Silverlight 5, DataContextProxy
不需要在的DataGrid
感谢支持的祖先结合。
示例
&LT;按钮命令={结合DataContext.CancelCommand,的RelativeSource = {的RelativeSource AncestorType = SDK:DataGrid中}}/&GT;
I have View(XAML included) attached to my ViewModel with commands. I need to invoke command when Button on DataGrid's row clicked. I'm using behaviors for this (regular commanding have same issue). When I click button on DataGrid - my command does not get fired.
To illustrate issue - I placed ListBox on a bottom with EXACT same binding stuff - and yes, command works. So, it's something with DataGrid/DataGridTemplateColumn
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding}">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Content="Cancel" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction MethodName="Cancel" TargetObject="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
<sdk:DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding Data}" Grid.Row="1">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Select">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ElementName=Control, Path=DataContext.ItemSelectedCommand}" CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn Binding="{Binding DeviceId}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Device" Width="Auto" FontWeight="Bold" />
<sdk:DataGridTextColumn Binding="{Binding SerialNumber}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Serial Number" Width="Auto" />
<sdk:DataGridTextColumn Binding="{Binding LastActivityOn}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Last Activity" Width="Auto" />
<sdk:DataGridTextColumn Binding="{Binding ClientVersion}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Client Version" Width="Auto" />
<sdk:DataGridTextColumn Binding="{Binding OSVersion}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="OS Version" Width="Auto" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
<ListBox Grid.Row="2" ItemsSource="{Binding Data}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding DeviceId}"></TextBlock>
<Button Content="Select">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ElementName=Control, Path=DataContext.ItemSelectedCommand}" CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I am pretty sure inside a DataGrid
, you still need the DataContextProxy to make bindings working. The ElementName binding doesn't work (yes, it is working for ListBox
templates, but not DataGrid
, this is because DataGridTemplateColumn is not in the visual tree), even in Silverlight 4.
UPDATE
In Silverlight 5, DataContextProxy
is not needed in a DataGrid
thanks to the support of Ancestor binding.
Example
<Button Command="{Binding DataContext.CancelCommand, RelativeSource={RelativeSource AncestorType=sdk:DataGrid}}" />
这篇关于结合内DataGridTemplateColumn命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!