类反序列化和 WPF 绑定

发布于 2024-12-04 08:18:43 字数 9978 浏览 0 评论 0原文

我有一个小难题,我正在尝试解决,但不知道如何解决它...

基于 MVVM 方法的 WPF 应用程序...

我有一个 SubstituionDataSet 类,它继承自 DataSet 并定义一个附加集合:

namespace Lib
{
    public class SubstitutionDataSet : DataSet
    {
        public SubstitutionDataSet()
        {
            TableNames = new ObservableCollection<SubstitutionDataTable>();

            Tables.CollectionChanging += DataTablesCollectionChanging;
        }

        public ObservableCollection<SubstitutionDataTable> TableNames { get; set; }

        private void DataTablesCollectionChanging(object sender, CollectionChangeEventArgs e)
        {
            var actionTable = (DataTable) e.Element;

            if (e.Action == CollectionChangeAction.Add)
            {
                actionTable.Columns.CollectionChanged += DataColumnCollectionChanged;
                TableNames.Add(new SubstitutionDataTable { Name = actionTable.TableName });
            }
            else if (e.Action == CollectionChangeAction.Remove)
            {
                actionTable.Columns.CollectionChanged -= DataColumnCollectionChanged;
                TableNames.Remove(TableNames.First(tn => tn.Name == actionTable.TableName));
            }
        }

        private void DataColumnCollectionChanged(object sender, CollectionChangeEventArgs e)
        {
            var actionColumn = (DataColumn) e.Element;
            var hostTable = (DataTable) actionColumn.Table;
            var hostSubsitutionTable = TableNames.First(tn => tn.Name == hostTable.TableName);

            if (e.Action == CollectionChangeAction.Add)
            {
                hostSubsitutionTable.ColumnNames.Add(actionColumn.ColumnName);
            }
            else if (e.Action == CollectionChangeAction.Remove)
            {
                 hostSubsitutionTable.ColumnNames.Remove(hostSubsitutionTable.ColumnNames.First(cn => cn == actionColumn.ColumnName));
            }
        }
    }
}

使用 SubstitutionDataTable定义如下:

namespace Lib
{
    public sealed class SubstitutionDataTable: INotifyPropertyChanged
    {
        private string _name;

        /// <summary>
        /// The <see cref="Name" /> property's name.
        /// </summary>
        private const string NamePropertyName = "Name";

        public SubstitutionDataTable()
        {
            ColumnNames = new ObservableCollection<string>();
        }

        /// <summary>
        /// Gets the Name property.
        /// Changes to that property's value raise the PropertyChanged event.
        /// </summary>
        public string Name
        {
            get
            {
                return _name;
            }

            set
            {
                if (_name == value)
                {
                    return;
                }

                _name = value;

                RaisePropertyChanged(NamePropertyName);
            }
        }

        public ObservableCollection<string> ColumnNames { get; set; }


        #region Implementation of INotifyPropertyChanged

        /// <summary>
        /// A property has changed - update bindings
        /// </summary>
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}

...现在这是难题的关键...

上面的类用于在 DataSet 中定义新的 DataTable 并添加列和行以及运行时。我有另一个允许配置混淆过程的类,部分配置允许从上面定义的 SubstituionDataSet 中选择 DataTable 和 DataColumn。

namespace Lib
{
    public class ObfuscationParams : INotifyPropertyChanged
    {
        private string _dataColumn;
        private string _dataTable;
        private char _maskCharacter;
        private int _numberCharacters;

        /// <summary>
        /// The <see cref="MaskCharacter" /> property's name.
        /// </summary>
        private const string MaskCharacterPropertyName = "MaskCharacter";
        /// <summary>
        /// The <see cref="DataColumn" /> property's name.
        /// </summary>
        private const string DataColumnPropertyName = "DataColumn";
        /// <summary>
        /// The <see cref="DataTable" /> property's name.
        /// </summary>
        private const string DataTablePropertyName = "DataTable";


        # region Mask Obfuscation Properties

        /// <summary>
        /// Defines whether whitespace is to be trimmed or not for a Mask obfuscation.
        /// </summary>
        public bool IsWhiteSpaceTrimmed { get; set; }

        /// <summary>
        /// Defines the mask character to be used for a Mask obfuscation.
        /// </summary>
        public char MaskCharacter
        {
            get { return _maskCharacter; }
            set
            {
                if (_maskCharacter == value)
                    return;

                _maskCharacter = value;

                RaisePropertyChanged(MaskCharacterPropertyName);
            }
        }

        /// <summary>
        /// Defines the number of masking characters to apply.
        /// </summary>
        public int NumberCharacters
        {
            get { return _numberCharacters; }
            set { _numberCharacters = value < 1 ? 1 : (value > 16 ? 16 : value); }
        }

        /// <summary>
        /// Defines the mask position for a Mask obfuscation.
        /// </summary>
        public MaskPosition MaskPosition { get; set; }

        #endregion


        # region Substitute Obfuscation Properties

        /// <summary>
        /// Defines which datacolumn is to be used for a Substitution obfuscation.
        /// </summary>
        public string DataColumn
        {
            get { return _dataColumn; }
            set
            {
                if (_dataColumn == value)
                    return;

                _dataColumn = value;

                RaisePropertyChanged(DataColumnPropertyName);
            }
        }

        /// <summary>
        /// Defines which datatable is to be used for a substitition obfuscation.
        /// </summary>
        public string DataTable
        {
            get { return _dataTable; }
            set
            {
                if (_dataTable == value)
                    return;

                _dataTable = value;

                RaisePropertyChanged(DataTablePropertyName);
                _dataTable = value;
            }
        }

        #endregion


        #region Implementation of INotifyPropertyChanged

        /// <summary>
        /// A property has changed - update bindings
        /// </summary>
        [field: NonSerialized]
        public virtual event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}

我的配置正常工作,可以配置许多混淆,然后将配置序列化到磁盘。

当我反序列化时,我发现 GUI 上的绑定没有显示正确的 DataTable 和 DataColumn 选择,DataTable 仅显示完全限定的对象名称。

我目前只是想让 DataTable 绑定工作 - 我知道我需要重新设计 DataColumn 绑定。

GUI(用户控件)定义如下:

<UserControl xmlns:igEditors="http://infragistics.com/Editors"  x:Class="SubstitutionOptions"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="421" d:DesignWidth="395">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="23" />
            <RowDefinition Height="23" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150" />
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0"
                   Grid.Column="0"
                   Text="Dataset" />
        <igEditors:XamComboEditor Grid.Row="0"
                                  Grid.Column="2"
                                  Name="tablesComboBox"
                                  NullText="select a dataset..."
                                  ItemsSource="{Binding DataContext.Project.SubstitutionDataSet.TableNames, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                                  DisplayMemberPath="Name"
                                  SelectedItem="{Binding DataContext.SelectedFieldSubstitutionDataTable, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>

        <TextBlock Grid.Row="1"
                   Grid.Column="0"
                   Text="Column" />
        <igEditors:XamComboEditor Grid.Row="1"
                                  Grid.Column="2"
                                  NullText="select a column..."
                                  ItemsSource="{Binding DataContext.SelectedFieldSubstitutionDataTable.ColumnNames, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                                  SelectedItem="{Binding DataColumn, Mode=TwoWay}"/>
    </Grid>
</UserControl>

我希望我已经充分解释了这个问题。有谁知道如何使用当前设计使其工作或重新设计方法以实现我的需要?

I have a little puzzle I'm trying to solve and am not sure how to go about it...

WPF application based on MVVM approach...

I have a SubstituionDataSet class that inherits from DataSet and defines an additional collection:

namespace Lib
{
    public class SubstitutionDataSet : DataSet
    {
        public SubstitutionDataSet()
        {
            TableNames = new ObservableCollection<SubstitutionDataTable>();

            Tables.CollectionChanging += DataTablesCollectionChanging;
        }

        public ObservableCollection<SubstitutionDataTable> TableNames { get; set; }

        private void DataTablesCollectionChanging(object sender, CollectionChangeEventArgs e)
        {
            var actionTable = (DataTable) e.Element;

            if (e.Action == CollectionChangeAction.Add)
            {
                actionTable.Columns.CollectionChanged += DataColumnCollectionChanged;
                TableNames.Add(new SubstitutionDataTable { Name = actionTable.TableName });
            }
            else if (e.Action == CollectionChangeAction.Remove)
            {
                actionTable.Columns.CollectionChanged -= DataColumnCollectionChanged;
                TableNames.Remove(TableNames.First(tn => tn.Name == actionTable.TableName));
            }
        }

        private void DataColumnCollectionChanged(object sender, CollectionChangeEventArgs e)
        {
            var actionColumn = (DataColumn) e.Element;
            var hostTable = (DataTable) actionColumn.Table;
            var hostSubsitutionTable = TableNames.First(tn => tn.Name == hostTable.TableName);

            if (e.Action == CollectionChangeAction.Add)
            {
                hostSubsitutionTable.ColumnNames.Add(actionColumn.ColumnName);
            }
            else if (e.Action == CollectionChangeAction.Remove)
            {
                 hostSubsitutionTable.ColumnNames.Remove(hostSubsitutionTable.ColumnNames.First(cn => cn == actionColumn.ColumnName));
            }
        }
    }
}

With the SubstitutionDataTable defined as below:

namespace Lib
{
    public sealed class SubstitutionDataTable: INotifyPropertyChanged
    {
        private string _name;

        /// <summary>
        /// The <see cref="Name" /> property's name.
        /// </summary>
        private const string NamePropertyName = "Name";

        public SubstitutionDataTable()
        {
            ColumnNames = new ObservableCollection<string>();
        }

        /// <summary>
        /// Gets the Name property.
        /// Changes to that property's value raise the PropertyChanged event.
        /// </summary>
        public string Name
        {
            get
            {
                return _name;
            }

            set
            {
                if (_name == value)
                {
                    return;
                }

                _name = value;

                RaisePropertyChanged(NamePropertyName);
            }
        }

        public ObservableCollection<string> ColumnNames { get; set; }


        #region Implementation of INotifyPropertyChanged

        /// <summary>
        /// A property has changed - update bindings
        /// </summary>
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}

...Now this is the crux of the puzzle...

The above classes are used to define a new DataTable within a DataSet and add columns and rows and run-time. I have another Class that allows configuration of an obfuscation process, part of the configuration allows selection of a DataTable and DataColumn from the SubstituionDataSet defined above.

namespace Lib
{
    public class ObfuscationParams : INotifyPropertyChanged
    {
        private string _dataColumn;
        private string _dataTable;
        private char _maskCharacter;
        private int _numberCharacters;

        /// <summary>
        /// The <see cref="MaskCharacter" /> property's name.
        /// </summary>
        private const string MaskCharacterPropertyName = "MaskCharacter";
        /// <summary>
        /// The <see cref="DataColumn" /> property's name.
        /// </summary>
        private const string DataColumnPropertyName = "DataColumn";
        /// <summary>
        /// The <see cref="DataTable" /> property's name.
        /// </summary>
        private const string DataTablePropertyName = "DataTable";


        # region Mask Obfuscation Properties

        /// <summary>
        /// Defines whether whitespace is to be trimmed or not for a Mask obfuscation.
        /// </summary>
        public bool IsWhiteSpaceTrimmed { get; set; }

        /// <summary>
        /// Defines the mask character to be used for a Mask obfuscation.
        /// </summary>
        public char MaskCharacter
        {
            get { return _maskCharacter; }
            set
            {
                if (_maskCharacter == value)
                    return;

                _maskCharacter = value;

                RaisePropertyChanged(MaskCharacterPropertyName);
            }
        }

        /// <summary>
        /// Defines the number of masking characters to apply.
        /// </summary>
        public int NumberCharacters
        {
            get { return _numberCharacters; }
            set { _numberCharacters = value < 1 ? 1 : (value > 16 ? 16 : value); }
        }

        /// <summary>
        /// Defines the mask position for a Mask obfuscation.
        /// </summary>
        public MaskPosition MaskPosition { get; set; }

        #endregion


        # region Substitute Obfuscation Properties

        /// <summary>
        /// Defines which datacolumn is to be used for a Substitution obfuscation.
        /// </summary>
        public string DataColumn
        {
            get { return _dataColumn; }
            set
            {
                if (_dataColumn == value)
                    return;

                _dataColumn = value;

                RaisePropertyChanged(DataColumnPropertyName);
            }
        }

        /// <summary>
        /// Defines which datatable is to be used for a substitition obfuscation.
        /// </summary>
        public string DataTable
        {
            get { return _dataTable; }
            set
            {
                if (_dataTable == value)
                    return;

                _dataTable = value;

                RaisePropertyChanged(DataTablePropertyName);
                _dataTable = value;
            }
        }

        #endregion


        #region Implementation of INotifyPropertyChanged

        /// <summary>
        /// A property has changed - update bindings
        /// </summary>
        [field: NonSerialized]
        public virtual event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}

I have the configuration working and can configure a number of obfuscations and then serialize the configuration to disk.

When I deserialize I find the bindings on the GUI don't show the correct DataTable and DataColumn selections, the DataTable just shows the fully qualified object name.

I am currently just trying to get the DataTable binding working - I know I need to rework the DataColumn binding.

The GUI (usercontrol) is defined as below:

<UserControl xmlns:igEditors="http://infragistics.com/Editors"  x:Class="SubstitutionOptions"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="421" d:DesignWidth="395">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="23" />
            <RowDefinition Height="23" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150" />
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0"
                   Grid.Column="0"
                   Text="Dataset" />
        <igEditors:XamComboEditor Grid.Row="0"
                                  Grid.Column="2"
                                  Name="tablesComboBox"
                                  NullText="select a dataset..."
                                  ItemsSource="{Binding DataContext.Project.SubstitutionDataSet.TableNames, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                                  DisplayMemberPath="Name"
                                  SelectedItem="{Binding DataContext.SelectedFieldSubstitutionDataTable, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>

        <TextBlock Grid.Row="1"
                   Grid.Column="0"
                   Text="Column" />
        <igEditors:XamComboEditor Grid.Row="1"
                                  Grid.Column="2"
                                  NullText="select a column..."
                                  ItemsSource="{Binding DataContext.SelectedFieldSubstitutionDataTable.ColumnNames, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                                  SelectedItem="{Binding DataColumn, Mode=TwoWay}"/>
    </Grid>
</UserControl>

I hope I have explained the problem sufficiently. Has anyone got any ideas on how I can either get it working using the current design or redesign the approach to achieve what I need?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2024-12-11 08:18:43

好吧,我想我现在已经破解了它,但似乎没有人感兴趣:-)

我会为后代发布答案...

我像这样更改了组合框的绑定...

<TextBlock Grid.Row="0"
            Grid.Column="0"
            Text="Dataset" />
<igEditors:XamComboEditor Grid.Row="0"
                            Grid.Column="2"
                            NullText="select a dataset..."
                            ItemsSource="{Binding DataContext.VDOProject.SubstitutionDataSet.TableNames, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                            DisplayMemberPath="Name"
                            Text="{Binding DataTable, Mode=TwoWay}"
                            SelectedItem="{Binding DataContext.SelectedFieldSubstitutionDataTable, Mode=OneWayToSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>

<TextBlock Grid.Row="1"
            Grid.Column="0"
            Text="Column" />
<igEditors:XamComboEditor Grid.Row="1"
                            Grid.Column="2"
                            NullText="select a column..."
                            ItemsSource="{Binding DataContext.SelectedFieldSubstitutionDataTable.ColumnNames, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                            Text="{Binding DataColumn, Mode=TwoWay}" />

OK, think I've cracked it now, not that anyone seems interested :-)

I'll post the answer for posterity though...

I changed the bindings for the Comboboxes like so...

<TextBlock Grid.Row="0"
            Grid.Column="0"
            Text="Dataset" />
<igEditors:XamComboEditor Grid.Row="0"
                            Grid.Column="2"
                            NullText="select a dataset..."
                            ItemsSource="{Binding DataContext.VDOProject.SubstitutionDataSet.TableNames, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                            DisplayMemberPath="Name"
                            Text="{Binding DataTable, Mode=TwoWay}"
                            SelectedItem="{Binding DataContext.SelectedFieldSubstitutionDataTable, Mode=OneWayToSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>

<TextBlock Grid.Row="1"
            Grid.Column="0"
            Text="Column" />
<igEditors:XamComboEditor Grid.Row="1"
                            Grid.Column="2"
                            NullText="select a column..."
                            ItemsSource="{Binding DataContext.SelectedFieldSubstitutionDataTable.ColumnNames, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                            Text="{Binding DataColumn, Mode=TwoWay}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文