Ticket T129527
Visible to All Users

How to pass CommandParameter in CellTemplate?

created 11 years ago
XAML
<Window x:Class="OddEvenRows.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" Height="400" Width="300" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" Title="JackSlater" x:Name="jackWindow"> <dxg:GridControl x:Name="gridControl" ItemsSource="{Binding MyList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="Index"> <dxg:GridColumn.CellTemplate> <DataTemplate> <dxe:ComboBoxEdit EditValue="{Binding Data.Index,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsTextEditable="False" > <dxe:ComboBoxEditItem x:Name="readyCombox" Content="ClickDownComboxItem"> <i:Interaction.Triggers> <i:EventTrigger EventName="PreviewMouseDown"> <!--Method 01--> <!--<cmd:EventToCommand Command="{Binding Path=View.DataContext.ImportCommand}" CommandParameter="{Binding Path=Title,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/>--> <!--Method 02--> <cmd:EventToCommand Command="{Binding Path=View.DataContext.ImportCommand}" CommandParameter="{Binding Path=Title,ElementName=jackWindow}"/> </i:EventTrigger> </i:Interaction.Triggers> </dxe:ComboBoxEditItem> </dxe:ComboBoxEdit> </DataTemplate> </dxg:GridColumn.CellTemplate> </dxg:GridColumn> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView AutoWidth="True"/> </dxg:GridControl.View> </dxg:GridControl> </Window>

I'd like to pass Title property in Window  as CommandParameter ,but CommandParameter is always to be null. Why ? How to solute it?

Comments (1)

    I've try Method01 && Method02 ,but failed.

    Answers approved by DevExpress Support

    created 11 years ago (modified 11 years ago)

    Hello,

    The ComboBoxEdit popup doesn't belong to the Window visual tree, therefore the binding your posted doesn't work. To accomplish this task, you can change your binding as follows:

    XAML
    <cmd:EventToCommand Command="{Binding Path=View.DataContext.ImportCommand}" CommandParameter="{Binding Path=View.DataControl.Parent.Title}"/>

    Please also note that if you are using GridColumn.CellTemplate, the most suitable solution to use it is to set the editors name to "PART_Editor" and remove its EditValue property binding. For example:

    XAML
    <dxg:GridColumn.CellTemplate> <DataTemplate> <dxe:ComboBoxEdit Name="PART_Editor" IsTextEditable="False" > <dxe:ComboBoxEditItem x:Name="readyCombox" Content="ClickDownComboxItem"> <i:Interaction.Triggers> <i:EventTrigger EventName="PreviewMouseDown"> <cmd:EventToCommand Command="{Binding Path=View.DataContext.ImportCommand}" CommandParameter="{Binding Path=View.DataControl.Parent.Title}"/> </i:EventTrigger> </i:Interaction.Triggers> </dxe:ComboBoxEditItem> </dxe:ComboBoxEdit> </DataTemplate> </dxg:GridColumn.CellTemplate>

    Please try this solution and let me know whether or not it meets your requirements.

    I am looking forward to your response.

    Thanks,
    Andrew

      Other Answers

      created 11 years ago (modified 11 years ago)

      Thanks you very much.Now, I may pass "Title" CommandParameter in CellTemplate
      I still has some questions.If I want to bind noTB01.Text property with CommandParameter.How to do it ?
      Here is my xaml.

      XAML
      <Window x:Class="OddEvenRows.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" Height="400" Width="300" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" Title="JackSlater" x:Name="jackWindow"> <StackPanel> <StackPanel> <TextBlock x:Name="noTB01" Text="No.1"></TextBlock> <TextBlock x:Name="noTB02" Text="No.2"></TextBlock> <Button x:Name="testBtn" Content="Hello"></Button> </StackPanel> <Grid> <dxg:GridControl x:Name="gridControl" Grid.Row="2" ItemsSource="{Binding MyList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="Index"> <dxg:GridColumn.CellTemplate> <DataTemplate> <dxe:ComboBoxEdit EditValue="{Binding Data.Index,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsTextEditable="False" > <dxe:ComboBoxEditItem x:Name="readyCombox" Content="ClickDownComboxItem"> <i:Interaction.Triggers> <i:EventTrigger EventName="PreviewMouseDown"> <cmd:EventToCommand Command="{Binding Path=View.DataContext.ImportCommand}" CommandParameter="{Binding Path=View.DataControl.Parent.Text}"/> </i:EventTrigger> </i:Interaction.Triggers> </dxe:ComboBoxEditItem> </dxe:ComboBoxEdit> </DataTemplate> </dxg:GridColumn.CellTemplate> </dxg:GridColumn> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView AutoWidth="True"/> </dxg:GridControl.View> </dxg:GridControl> </Grid> </StackPanel> </Window>
        Comments (3)
        Andrey K (DevExpress Support) 11 years ago

          The most flexible ways to get an element that is not in the same visible tree are described in these articles:

          [WPF] How to bind to data when the DataContext is not inherited
          WPF Error: Cannot find govering FrameworkElement for target element

          I have changed your example to show one of them in action. Please take a look at the attachment and let me know your results.

          Thanks,
          Andrew

            Thank you very much !!!

            Andrey K (DevExpress Support) 11 years ago

              You are welcome. I am happy to hear that my assistance was helpful. Thank you for keeping me informed.

              Andrew.

              Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

              Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.