繁体   English   中英

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

[英]Selecting an Item in a ComboBox affects an unrelated ComboBox

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

  1. CashActivityTypeBAI CashActivityTypesCombo ,它们绑定到我的CashActivityTypes DataView
  2. CashActivitiesBAI绑定到我的BAICashActivites DataView
  3. CashActivitiesCombo绑定到我的CashActivities DataView。

我包括了我的XAML和三个类:

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

我的问题

CashActivityTypes ComboBox项中的任何一个项被选中时,它们各自的子DataView将被过滤。 但是,更改CashActivityTypeBAICashActivityTypesCombo ,它将同时为两个CashActivity组合框过滤两个子DataView。

我的XAML

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

ImportPresenterPresenter类的子级

namespace 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 

    }
}

因此,我想我已经发现了发生了什么事情,如果有人遇到这个问题。

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

因此,我没有使用同一DataTable来创建新变量,而是使用了DataTable的TableAdapterGetData()函数,效果很好。

private DataView _MyView1 = TableAdapter1.GetData().DefaultView;
private DataView _MyView2 = TableAdapter1.GetData().DefaultView;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM