选择ComboBox中的项会影响不相关的ComboBox

时间:2018-04-05 21:49:42

标签: c# wpf xaml mvvm

我有一个XAML ImportPresenter,它连接到ComboBox ViewModel。我的XAML中有四个CashActivityTypeBAI个项目:

  1. CashActivityTypesCombo CashActivityTypes,这是必然的 我的CashActivitiesBAI DataView
  2. BAICashActivites哪个绑定到CashActivitiesCombo 数据视图
  3. CashActivities哪个绑定到Presenter 数据视图。
  4. 我已经包含了我的XAML和三个类:

    ImportPresenterController是我的ViewModel。有一个静态CashActivityTypes类,它用作我的模型,也连接到我的DataSet。

    我的问题

    CashActivityTypeBAI ComboBox项目中的任何一个选择项目时,将过滤其各自的子DataView。但是,当更改CashActivityTypesComboCashActivity时,它会同时过滤两个<UserControl x:Class="Cash_Sheet_WPF.Views.Pages.CheckBAIPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Cash_Sheet_WPF.Views.Pages" xmlns:UserControls="clr-namespace:Cash_Sheet_WPF.Views.UserControls" xmlns:ViewModels="clr-namespace:Cash_Sheet_WPF.ViewModels" mc:Ignorable="d" d:DesignHeight="534" d:DesignWidth="1184"> <Grid> <Grid.DataContext> <ViewModels:ImportPresenter/> </Grid.DataContext> <Grid.Background> <ImageBrush ImageSource="/Cash Sheet WPF;component/backgroundA.png" Stretch="UniformToFill"/> </Grid.Background> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="20"/> <RowDefinition Height="44"/> <RowDefinition Height="2*" MaxHeight="800"/> <RowDefinition Height="24"/> </Grid.RowDefinitions> <UserControls:MainMenu Grid.Row="0" VerticalAlignment="Top"/> <Grid Grid.Row="2" Margin="5,2,5,2" Width="Auto" Height="Auto" VerticalAlignment="Stretch"> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="2*"/> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <Grid Grid.Column="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="24"/> <RowDefinition Height="24"/> <RowDefinition Height="24"/> <RowDefinition Height="24"/> <RowDefinition Height="2*"/> </Grid.RowDefinitions> <Label Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" Height="20" FontSize="9" Content="Cash Activity Type"/> <ComboBox x:Name="CashActivityTypeBAI" Grid.Column="1" Grid.Row="0" Height="20" FontSize="9" ItemsSource="{Binding CashActivityTypes, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Type" SelectedValue="{Binding SelectedBAIType}" SelectedValuePath="Sequence" SelectedIndex="{Binding CashActivityTypeIndex, UpdateSourceTrigger=PropertyChanged}" Margin="0,2,0,0"/> <Label Grid.Column="0" Grid.Row="1" HorizontalAlignment="Right" Height="20" FontSize="9" Content="Cash Activity"/> <Label Grid.Column="1" Grid.Row="1" Height="20" FontSize="9" Content="{Binding SelectedActivity, UpdateSourceTrigger=PropertyChanged}"/> <ComboBox x:Name="CashActivitiesBAI" Grid.Column="1" Grid.Row="1" Height="20" FontSize="9" ItemsSource="{Binding BAICashActivities, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Activity" SelectedValue="{Binding SelectedBAIActivity}" SelectedValuePath="Sequence" SelectedIndex="{Binding CashActivityIndex, UpdateSourceTrigger=PropertyChanged}" Margin="0,2,0,0"/> </Grid> <Grid Grid.Column="2" Margin="2,0,0,0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="2*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="24"/> <RowDefinition Height="24"/> <RowDefinition Height="24"/> <RowDefinition Height="24"/> <RowDefinition Height="24"/> <RowDefinition Height="75"/> <RowDefinition Height="24"/> <RowDefinition Height="2*"/> </Grid.RowDefinitions> <Label Grid.Column="0" Grid.Row="0" Content="Individual Transactions" FontSize="9"/> <Label Content="Cash Activity Type" FontSize="9" Grid.Row="1" Grid.Column="0" Margin="0,0,2,2" HorizontalAlignment="Right"/> <ComboBox x:Name="CashActivityTypesCombo" Grid.Row="1" Grid.Column="1" Width="Auto" Height="Auto" MaxHeight="20" MinHeight="20" Background="White" ItemsSource="{Binding CashActivityTypes, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Type" SelectedValue="{Binding TypeSequence}" SelectedValuePath="Sequence" Margin="2,0,0,2" HorizontalAlignment="Stretch"/> <Label Content="Cash Activity" FontSize="9" Grid.Row="2" Grid.Column="0" Margin="0,2,2,2" HorizontalAlignment="Right"/> <ComboBox x:Name="CashActivitiesCombo" Grid.Row="2" Grid.Column="1" Width="Auto" Height="Auto" MaxHeight="20" MinHeight="20" Background="White" ItemsSource="{Binding CashActivities, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Activity" SelectedValue="{Binding ActivitySequence}" SelectedValuePath="Sequence" Margin="2,2,0,2" HorizontalAlignment="Stretch"/> </Grid> </Grid> </Grid> </UserControl> 组合框的子DataView。

    我的XAML

    ImportPresenter

    Presenternamespace Cash_Sheet_WPF.ViewModels { /// <summary> /// Main Presenter Class - Parent class of other presenters /// </summary> public class Presenter : ObservableObject { #region Variables protected DataView _CashActivityTypes = Controller.FinanceDB.CashActivityType.DefaultView; protected DataView _CashActivities = Controller.FinanceDB.CashActivity.DefaultView; protected short _TypeSequence; protected short _ActivitySequence; #endregion #region Bindings //Returns a DataView from the CashActivityType table public DataView CashActivityTypes { get { return _CashActivityTypes; } set { _CashActivityTypes = value; RaisePropertyChangedEvent("CashActivityTypes"); } }//END CASHACTIVITYTYPES public DataView CashActivities { get { return _CashActivities; } set { _CashActivities = value; RaisePropertyChangedEvent("CashActivities"); } }//END CASHACTIVITIES public short TypeSequence { get { return _TypeSequence; } set { _TypeSequence = value; CashActivities.RowFilter = "Seq_CashActivityType = " + _TypeSequence; RaisePropertyChangedEvent("TypeSequence"); } }//END TYPESEQUENCE #endregion } } 班级的孩子

    ImportPresenter

    Presenter类,扩展namespace Cash_Sheet_WPF.ViewModels { public class ImportPresenter : Presenter { #region Variables //BAI Adjustment private DataView _BAI; private DataRowView _BAIRow; private DataView _BAICashActivities = Controller.FinanceDB.CashActivity.DefaultView; private short _SelectedBAIType = 0; private short _SelectedBAIActivity = 0; private long _CashActivityTypeIndex = 0; private long _CashActivityIndex = 0; private decimal _BucketAmount = 0; #endregion #region Bindings /// <summary> /// Cash Activities for the BAI Import Adjustment Section /// </summary> public DataView BAICashActivities { get { return _BAICashActivities; } set { _BAICashActivities = value; RaisePropertyChangedEvent("BAICashActivities"); } } /// <summary> /// Gets the Selected Type index, to be used by the Combobox /// </summary> public long CashActivityTypeIndex { get { return _CashActivityTypeIndex; } set { _CashActivityTypeIndex = value; RaisePropertyChangedEvent("CashActivityTypeIndex"); } } /// <summary> /// Stores the index of the currently selected Cash Activity /// </summary> public long CashActivityIndex { get { return _CashActivityIndex; } set { _CashActivityIndex = value; RaisePropertyChangedEvent("CashActivityIndex"); } } /// <summary> /// Selected Cash Activity Type /// </summary> public short SelectedBAIType { get { return _SelectedBAIType; } set { _SelectedBAIType = value; RaisePropertyChangedEvent("SelectedBAIType"); BAICashActivities.RowFilter = "Seq_CashActivityType = " + _SelectedBAIType; RaisePropertyChangedEvent("BAICashActivities"); } } /// <summary> /// The Selected Cash Activity /// </summary> public short SelectedBAIActivity { get { return _SelectedBAIActivity; } set { _SelectedBAIActivity = value; RaisePropertyChangedEvent("SelectedBAIActivity"); } } /// <summary> /// Returns the Index of text in a DataView /// </summary> /// <param name="dataView">DataView you are searching</param> /// <param name="stringItem">Search Text</param> /// <returns></returns> private long GetSelectedIndex(DataView dataView, string stringItem) { long selectedIndex = 0; foreach(DataRowView dataRowView in dataView) { if (dataRowView.Row.ItemArray.Contains(stringItem)) { break; } else { selectedIndex++; } } return selectedIndex; } #endregion } }

    {{1}}

1 个答案:

答案 0 :(得分:0)

所以我想如果有人遇到这个问题,我已经发现了正在发生的事情。

当您基于相同的DataView对象创建两个不同的DataTable对象时,一个上的过滤器将始终影响另一个。

因此,我没有使用同一个DataTable创建新变量,而是使用了DataTable GetData()的{​​{1}}函数,这很好。

TableAdapter