2012-10-08 192 views
0

我在DataGrid中使用級聯組合框。我能夠基於selectionchanged獲取數據,但是該事件針對每一行觸發。 這裏是我的代碼: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

我有同樣的問題在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 
       } 
     } 

    } 
} 

使用測試命名空間中的用戶控件組件

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> 
+0

它並不能幫助我,但我將其標記爲答案,因爲它給了一些想法。 – ritesh

相關問題