每行的Selectionchanged事件触发

时间:2012-10-08 06:08:12

标签: silverlight xaml mvvm datagrid

我在datagrid中使用级联组合框。我能够根据selectionchanged获取数据,但该事件是针对每一行触发的。 这是我的代码:

<sdk:datagridtemplatecolumn header="Category" width="110">

    <sdk:datagridtemplatecolumn.celltemplate>
        <datatemplate>                                    
            <combobox foreground="Black" height="30" isenabled="{Binding Source={StaticResource EffortViewModel}, Path=ComboBoxStatus}" itemssource="{Binding Source={StaticResource EffortViewModel},Path=ProjTypeTaskCtry}" displaymemberpath="TaskCtgyName" selectedvaluepath="TaskCtgy_FK" selectedvalue="{Binding Source={StaticResource EffortViewModel}, Path=TaskCtgy_FKField,Mode=TwoWay}" />
        </datatemplate>
    </sdk:datagridtemplatecolumn.celltemplate>
</sdk:datagridtemplatecolumn>

<sdk:datagridtemplatecolumn header="SubCategory" width="110">

    <sdk:datagridtemplatecolumn.celltemplate>
        <datatemplate>
            <combobox foreground="Black" height="30" isenabled="{Binding Source={StaticResource EffortViewModel}, Path=ComboBoxStatus}" itemssource="{Binding Source={StaticResource EffortViewModel},Path=SubCtry,Mode=OneWay}" displaymemberpath="TaskSubCtgyName" selectedvaluepath="{Binding TaskSubCtgy_PK, Mode=TwoWay}" selectedvalue="{Binding TaskSubCtgy_FKField,Mode=OneTime}" selectedindex="{Binding TaskSubCtgy_FKField}" /> 
        </datatemplate>
    </sdk:datagridtemplatecolumn.celltemplate>
</sdk:datagridtemplatecolumn>

1 个答案:

答案 0 :(得分:1)

我在Silverlight MVVM中遇到了同样的问题。我从某个地方找到了解决方案。希望这会对你有所帮助。

namespace Test
{

     public class ComboBoxSelectionChange : TriggerAction<DependencyObject>
         {
           public ComboBoxSelectionChange()
            {
            }
       public ComboBox DayComboBox
       {
         get { return (ComboBox)GetValue(DayComboBoxProperty); }
         set { SetValue(DayComboBoxProperty, value); }
        }



       public static readonly DependencyProperty DayComboBoxProperty =
                         DependencyProperty.Register("DayComboBox",
                         typeof(ComboBox),
                         typeof(ComboBoxSelectionChange),
                         new PropertyMetadata(null, OnDayComboBoxPropertyChanged));

       private static void OnDayComboBoxPropertyChanged(DependencyObjectd, DependencyPropertyChangedEventArgs e)
          {
             var source = d as ComboBoxSelectionChange;
             if (source != null)
              {
                var value = (ComboBox)e.NewValue;
              }
          }

       protected override void Invoke(object o)
         {
            if (this.DayComboBox != null)
              {
                //this method will execute when the selection is changed
              }
         }

     }
 }

在Usercontrol程序集中使用Test命名空间

  xmlns:Common="clr-namespace:Test"

  <UserControl.Resources>
        <Common:ComboBoxSelectionChange x:Name="ComboBoxItem"/>
  </UserControl.Resources>


     <DataTemplate x:Key="EditMondayDataTemplate">
        <ComboBox x:Name="cmbMonday" Height="26" Margin="3" ItemsSource="{Binding Monday,Mode=OneTime}" DisplayMemberPath="displayText" SelectedItem="{Binding Path=MonSelected,Mode=TwoWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="80">
            <i:Interaction.Triggers>
                  <i:EventTrigger EventName="SelectionChanged">
                        <Common:ComboBoxSelectionChange DayComboBox="{Binding ElementName=cmbMonday}" TextParam="Monday"/>
                  </i:EventTrigger>
            </i:Interaction.Triggers>
        </ComboBox>         
    </DataTemplate>