开发者

Command binding inside a DataGridTemplateColumn

I am working on a Silverlight application which makes an extensive use of Prism, the MVVM pattern and MEF. For several reasons, I have c开发者_如何学Gohosen to follow a View-first approach.

In one of the Views there is a DataGrid, and one of the columns of this grid is a DataGridTemplateColumn, which has just a Button.

I'd like to define both a Command and a CommandParameter on the Button. The Command should be a DelegateCommand of the ViewModel. CommandParameter should be the SelectedItems list coming straight out of the dataGrid.

I've tried several approaches to do this, but either Command or CommandParameter are null.

It follows the code that I originally wrote:

<sdk:DataGridTemplateColumn>
    <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Width="15" Height="15" Content=">" 
                    Command="{Binding UpdateSearchParametersCommand}" 
                    CommandParameter="{Binding SelectedItems, ElementName=dataGrid}">
        </DataTemplate>
    </sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

Could someone advice me on what the best way to go about it is?

Thanks in advance, Gianluca.


Your current binding is pointing to DataGridRowItem.UpdateSearchParametersCommand. You need to change it to point to DataGrid.DataContext.UpdateSearchParametersCommand

<sdk:DataGrid x:Name=dataGrid>
    <sdk:DataGridTemplateColumn>
        <sdk:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Width="15" Height="15" Content=">" 
                        Command="{Binding DataContext.UpdateSearchParametersCommand, ElementName=dataGrid}" 
                        CommandParameter="{Binding SelectedItems, ElementName=dataGrid}">
            </DataTemplate>
        </sdk:DataGridTemplateColumn.CellTemplate>
    </sdk:DataGridTemplateColumn>
</sdk:DataGrid>


If you bind your DataGrid using ItemsSource, then Command and CommandParameter binding is associated to the current item - the way you've written.

You should use alternative source in this case. Command should be binded to the DataContext.UpdateSearchParametersCommand and CommandParameter - to DataContext.SelectedItems.

In your case neither UpdateSearchParametersCommand, nor SelectedItems cannot be found in the binded item.

UPDATED

Be sure to set the right type for ancestor. I've set it to window, but maybe you are using UserControl.

<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Button Width="15" Height="15" Content=">" 
                Command="{Binding Path=DataContext.UpdateSearchParametersCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" 
                CommandParameter="{Binding Path=DataContext.SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
    </DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>


In silverlight 5 you can do this

<Button Command="{Binding Path=DataContext.PreviewPublishCommand, RelativeSource={RelativeSource AncestorType=controls:ChildWindow}}" Content="Publish" />

Just adjust AncestorType to be whatever your top level element is (UserControl, ChildWindow, etc).


Many of you tried to help me out on this. Thank you for that. Unfortunately the provided answers were mostly relative to WPF.

Here is how I've solved the problem:

<helpers:BindingHelper.Binding>
<helpers:BindingList>
     <helpers:RelativeSourceBinding TargetProperty="Command" Path="DataContext.ToggleDataArchiveInheritanceCommand" RelativeMode="FindAncestor" AncestorType="ChildWindow" />
</helpers:BindingList>
</helpers:BindingHelper.Binding>

Ok, this comes from another point of the same application, but the principle is the same. If a binding is defined inside a , the only way you have in Silverlight to reach out other elements that normally would be out-of-scope (as they are not part of the DataTemplate) is to walk through the xaml object tree. That's what the BindingHelper does.

Posting here as I hope the information will be useful to someone else.

Cheers,

Gianluca

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜