0

我在 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>
4

1 回答 1

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>
于 2012-10-08T07:38:21.233 回答