如何从自定义用户控件 WPF、C# 中的枚举自定义 属性 获取值?
How to get the value from an enum custom property in custom usercontrol WPF,C#?
我正在创建一个简单的用户控件,它具有 3 个自定义属性:其中两个工作正常,但我对第三个枚举进行了故障排除。
我的用户控件是一个组合框,它显示与数据库的连接(MySql、Sql 服务器...),我想显示全部、MySql 或 SqlServer由于 TypeOfDatabase 属性.
等连接
我的问题是我的 TypeOfDatabase 属性 等于 BaseConnection.xaml.cs 开关中的“全部”,因此我不能只有 MySql 个连接。
我是 WPF 的新手,我不知道我必须做什么才能获取值集。
我希望你能解释我做错了什么并给我一个解决方案。
提前致谢。
没有更多的文字,但我的代码会有所帮助...
我的用户控件:
BaseConnection.xaml
<UserControl x:Class="WpfCommonControls.Utils.UserControls.BaseConnection"
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:WpfCommonControls.Utils.UserControls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="400"
x:Name="BaseConnectionUserControl">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/UserControlsResources.en-EN.xaml"/>
<ResourceDictionary Source="Resources/UserControlsResources.fr-FR.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<Label x:Name="lbl_Connection_uc" Padding="0"
Content="{DynamicResource LabelBaseConnection}"/>
<ComboBox x:Name="cbbox_Connections_uc" Margin="0 5 5 0"
ItemsSource="{Binding ElementName=BaseConnectionUserControl, Path=UcConnectionList}"
SelectedValuePath="Name"
SelectedValue="{Binding ElementName=BaseConnectionUserControl, Path=UcConnectionName, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name">
</ComboBox>
</StackPanel>
</Grid>
</UserControl>
BaseConnection.xaml.cs
namespace WpfCommonControls.Utils.UserControls
{
/// <summary>
/// Interaction logic for BaseConnection.xaml
/// </summary>
public partial class BaseConnection : UserControl
{
#region Properties
public static readonly DependencyProperty ConnectionNameProperty = DependencyProperty.Register("UcConnectionName", typeof(String), typeof(BaseConnection));
public String UcConnectionName
{
get
{
return (String)GetValue(ConnectionNameProperty);
}
set
{
SetValue(ConnectionNameProperty, value);
}
}
public static readonly DependencyProperty ConnectionListProperty = DependencyProperty.Register("UcConnectionList", typeof(List<UcConnection>), typeof(BaseConnection));
public List<UcConnection> UcConnectionList
{
get
{
return (List<UcConnection>)GetValue(ConnectionListProperty);
}
set
{
SetValue(ConnectionListProperty, value);
}
}
public static readonly DependencyProperty TypeOfDatabaseProperty = DependencyProperty.Register("TypeOfDatabase", typeof(DataBaseType), typeof(BaseConnection));
public DataBaseType TypeOfDatabase
{
get
{
return (DataBaseType)GetValue(TypeOfDatabaseProperty);
}
set
{
SetValue(TypeOfDatabaseProperty, value);
}
}
#endregion Properties
#region Private Properties
private String _languageCode = "en-EN";
private CultureHelper _cultureHelper = new CultureHelper();
private UcConnection _ucConnection = new UcConnection();
#endregion Private Properties
public BaseConnection()
{
InitializeComponent();
_languageCode = Thread.CurrentThread.CurrentCulture.Name;
this.Resources = _cultureHelper.FindRessourcesDictionnary(this.Resources, "UserControlsResources", _languageCode);
List<UcConnection> connectionListByDatabaseType = new List<UcConnection>();
switch (TypeOfDatabase)
{
case DataBaseType.MySql:
foreach (UcConnection ucConnection in UcConnectionList)
{
if (ucConnection.DriverName.ToLower().Contains("mysql"))
{
connectionListByDatabaseType.Add(ucConnection);
}
}
UcConnectionList = connectionListByDatabaseType;
break;
case DataBaseType.SqlServer:
break;
default:
break;
}
}
public BaseConnection(String connectionName, List<UcConnection> connections)
{
InitializeComponent();
UcConnectionList = connections;
connectionName = UcConnectionName;
_languageCode = Thread.CurrentThread.CurrentCulture.Name;
this.Resources = _cultureHelper.FindRessourcesDictionnary(this.Resources, "UserControlsResources", _languageCode);
}
}
}
我的枚举
UserControlEnum.cs
namespace WpfCommonControls.Utils.UserControls
{
/// <summary>
/// Enumeration for the databasetype to display in the base connection combobox
/// </summary>
public enum DataBaseType
{
[Description("All available connections")]
All = 0,
[Description("Only MySql connections")]
MySql = 1,
[Description("Only Sql server connections")]
SqlServer = 2
}
}
我的 UcConnection class
UcConnection.cs
namespace WpfCommonControls.CommonClasses
{
public class UcConnection
{
public String Name { get; set; }
public String ServerName { get; set; }
public String ServerPort { get; set; }
public String DatabaseName { get; set; }
public String UserName { get; set; }
public String UserPassword { get; set; }
public String DriverName { get; set; }
public UcConnection()
{
}
public UcConnection(String nameConnection, String serverName, String serverPort, String databaseName, String userName, String userPassword, String driverName)
{
Name = nameConnection;
ServerName = serverName;
ServerPort = serverPort;
DatabaseName = databaseName;
UserName = userName;
UserPassword = userPassword;
DriverName = driverName;
}
public List<UcConnection> UcConnections
{
get
{
return _UcConnections;
}
set
{
_UcConnections = value;
}
}
private List<UcConnection> _UcConnections = new List<UcConnection>();
public static UcConnection GetUcConnection(String name, List<UcConnection> connections)
{
UcConnection selectedConnection = new UcConnection();
if (connections == null)
{
connections = new List<UcConnection>();
}
foreach (UcConnection cn in connections)
{
if (cn.Name == name)
{
selectedConnection = cn;
break;
}
}
return selectedConnection;
}
}
}
我如何在 window
中使用我的用户控件
xmlns:commonUserControls="clr-namespace:WpfCommonControls.Utils.UserControls;assembly=WpfCommonControls"
<commonUserControls:BaseConnection x:Name="uc_BaseConnection"
UcConnectionList="{Binding Path=ConnectionList}"
UcConnectionName="{Binding Path = ConnectionName, UpdateSourceTrigger = Explicit, Mode=TwoWay}"
TypeOfDatabase="MySql"/>
在BaseConnection.xaml.cs中,我已经试过了,但没有解决问题
public static readonly DependencyProperty TypeOfDatabaseProperty = DependencyProperty.Register("TypeOfDatabase", typeof(DataBaseType), typeof(BaseConnection), new PropertyMetadata(DataBaseType.All, new PropertyChangedCallback(OnTypeOfDatabaseChanged)));
private static void OnTypeOfDatabaseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (BaseConnection)d;
control.TypeOfDatabase = (DataBaseType)e.NewValue;
}
public DataBaseType TypeOfDatabase
{
get
{
return (DataBaseType)GetValue(TypeOfDatabaseProperty);
}
set
{
SetValue(TypeOfDatabaseProperty, value);
}
}
首先,我不建议您在 DPChanged 回调中更改依赖项 属性 的值。然后你在构造函数中所做的你必须在 DPChanged 回调中做。
这应该有效:
public DataBaseType TypeOfDatabase
{
get
{
return (DataBaseType)GetValue(TypeOfDatabaseProperty);
}
set
{
SetValue(TypeOfDatabaseProperty, value);
}
}
public static readonly DependencyProperty TypeOfDatabaseProperty = DependencyProperty.Register(nameof(TypeOfDatabase), typeof(DataBaseType), typeof(BaseConnection), new PropertyMetadata(DataBaseType.All, new PropertyChangedCallback(OnTypeOfDatabaseChanged)));
private static void OnTypeOfDatabaseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var connectionListByDatabaseType = new List<UcConnection>();
switch((DataBaseType)e.NewValue)
{
case DataBaseType.MySql:
foreach(UcConnection ucConnection in UcConnectionList)
{
if(ucConnection.DriverName.ToLower().Contains("mysql"))
{
connectionListByDatabaseType.Add(ucConnection);
}
}
UcConnectionList = connectionListByDatabaseType;
break;
case DataBaseType.SqlServer:
break;
default:
break;
}
}
您在设置 属性 之前在构造函数中打开 TypeOfDatabase
。
将 switch
语句移动到 OnTypeOfDatabaseChanged
回调或 Loaded
事件处理程序:
public BaseConnection()
{
InitializeComponent();
_languageCode = Thread.CurrentThread.CurrentCulture.Name;
this.Resources = _cultureHelper.FindRessourcesDictionnary(this.Resources, "UserControlsResources", _languageCode);
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
List<UcConnection> connectionListByDatabaseType = new List<UcConnection>();
switch (TypeOfDatabase)
{
case DataBaseType.MySql:
foreach (UcConnection ucConnection in UcConnectionList)
{
if (ucConnection.DriverName.ToLower().Contains("mysql"))
{
connectionListByDatabaseType.Add(ucConnection);
}
}
UcConnectionList = connectionListByDatabaseType;
break;
case DataBaseType.SqlServer:
break;
default:
break;
}
}
我正在创建一个简单的用户控件,它具有 3 个自定义属性:其中两个工作正常,但我对第三个枚举进行了故障排除。 我的用户控件是一个组合框,它显示与数据库的连接(MySql、Sql 服务器...),我想显示全部、MySql 或 SqlServer由于 TypeOfDatabase 属性.
等连接我的问题是我的 TypeOfDatabase 属性 等于 BaseConnection.xaml.cs 开关中的“全部”,因此我不能只有 MySql 个连接。
我是 WPF 的新手,我不知道我必须做什么才能获取值集。
我希望你能解释我做错了什么并给我一个解决方案。 提前致谢。
没有更多的文字,但我的代码会有所帮助...
我的用户控件:
BaseConnection.xaml
<UserControl x:Class="WpfCommonControls.Utils.UserControls.BaseConnection"
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:WpfCommonControls.Utils.UserControls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="400"
x:Name="BaseConnectionUserControl">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/UserControlsResources.en-EN.xaml"/>
<ResourceDictionary Source="Resources/UserControlsResources.fr-FR.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<Label x:Name="lbl_Connection_uc" Padding="0"
Content="{DynamicResource LabelBaseConnection}"/>
<ComboBox x:Name="cbbox_Connections_uc" Margin="0 5 5 0"
ItemsSource="{Binding ElementName=BaseConnectionUserControl, Path=UcConnectionList}"
SelectedValuePath="Name"
SelectedValue="{Binding ElementName=BaseConnectionUserControl, Path=UcConnectionName, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name">
</ComboBox>
</StackPanel>
</Grid>
</UserControl>
BaseConnection.xaml.cs
namespace WpfCommonControls.Utils.UserControls
{
/// <summary>
/// Interaction logic for BaseConnection.xaml
/// </summary>
public partial class BaseConnection : UserControl
{
#region Properties
public static readonly DependencyProperty ConnectionNameProperty = DependencyProperty.Register("UcConnectionName", typeof(String), typeof(BaseConnection));
public String UcConnectionName
{
get
{
return (String)GetValue(ConnectionNameProperty);
}
set
{
SetValue(ConnectionNameProperty, value);
}
}
public static readonly DependencyProperty ConnectionListProperty = DependencyProperty.Register("UcConnectionList", typeof(List<UcConnection>), typeof(BaseConnection));
public List<UcConnection> UcConnectionList
{
get
{
return (List<UcConnection>)GetValue(ConnectionListProperty);
}
set
{
SetValue(ConnectionListProperty, value);
}
}
public static readonly DependencyProperty TypeOfDatabaseProperty = DependencyProperty.Register("TypeOfDatabase", typeof(DataBaseType), typeof(BaseConnection));
public DataBaseType TypeOfDatabase
{
get
{
return (DataBaseType)GetValue(TypeOfDatabaseProperty);
}
set
{
SetValue(TypeOfDatabaseProperty, value);
}
}
#endregion Properties
#region Private Properties
private String _languageCode = "en-EN";
private CultureHelper _cultureHelper = new CultureHelper();
private UcConnection _ucConnection = new UcConnection();
#endregion Private Properties
public BaseConnection()
{
InitializeComponent();
_languageCode = Thread.CurrentThread.CurrentCulture.Name;
this.Resources = _cultureHelper.FindRessourcesDictionnary(this.Resources, "UserControlsResources", _languageCode);
List<UcConnection> connectionListByDatabaseType = new List<UcConnection>();
switch (TypeOfDatabase)
{
case DataBaseType.MySql:
foreach (UcConnection ucConnection in UcConnectionList)
{
if (ucConnection.DriverName.ToLower().Contains("mysql"))
{
connectionListByDatabaseType.Add(ucConnection);
}
}
UcConnectionList = connectionListByDatabaseType;
break;
case DataBaseType.SqlServer:
break;
default:
break;
}
}
public BaseConnection(String connectionName, List<UcConnection> connections)
{
InitializeComponent();
UcConnectionList = connections;
connectionName = UcConnectionName;
_languageCode = Thread.CurrentThread.CurrentCulture.Name;
this.Resources = _cultureHelper.FindRessourcesDictionnary(this.Resources, "UserControlsResources", _languageCode);
}
}
}
我的枚举 UserControlEnum.cs
namespace WpfCommonControls.Utils.UserControls
{
/// <summary>
/// Enumeration for the databasetype to display in the base connection combobox
/// </summary>
public enum DataBaseType
{
[Description("All available connections")]
All = 0,
[Description("Only MySql connections")]
MySql = 1,
[Description("Only Sql server connections")]
SqlServer = 2
}
}
我的 UcConnection class UcConnection.cs
namespace WpfCommonControls.CommonClasses
{
public class UcConnection
{
public String Name { get; set; }
public String ServerName { get; set; }
public String ServerPort { get; set; }
public String DatabaseName { get; set; }
public String UserName { get; set; }
public String UserPassword { get; set; }
public String DriverName { get; set; }
public UcConnection()
{
}
public UcConnection(String nameConnection, String serverName, String serverPort, String databaseName, String userName, String userPassword, String driverName)
{
Name = nameConnection;
ServerName = serverName;
ServerPort = serverPort;
DatabaseName = databaseName;
UserName = userName;
UserPassword = userPassword;
DriverName = driverName;
}
public List<UcConnection> UcConnections
{
get
{
return _UcConnections;
}
set
{
_UcConnections = value;
}
}
private List<UcConnection> _UcConnections = new List<UcConnection>();
public static UcConnection GetUcConnection(String name, List<UcConnection> connections)
{
UcConnection selectedConnection = new UcConnection();
if (connections == null)
{
connections = new List<UcConnection>();
}
foreach (UcConnection cn in connections)
{
if (cn.Name == name)
{
selectedConnection = cn;
break;
}
}
return selectedConnection;
}
}
}
我如何在 window
中使用我的用户控件xmlns:commonUserControls="clr-namespace:WpfCommonControls.Utils.UserControls;assembly=WpfCommonControls"
<commonUserControls:BaseConnection x:Name="uc_BaseConnection"
UcConnectionList="{Binding Path=ConnectionList}"
UcConnectionName="{Binding Path = ConnectionName, UpdateSourceTrigger = Explicit, Mode=TwoWay}"
TypeOfDatabase="MySql"/>
在BaseConnection.xaml.cs中,我已经试过了,但没有解决问题
public static readonly DependencyProperty TypeOfDatabaseProperty = DependencyProperty.Register("TypeOfDatabase", typeof(DataBaseType), typeof(BaseConnection), new PropertyMetadata(DataBaseType.All, new PropertyChangedCallback(OnTypeOfDatabaseChanged)));
private static void OnTypeOfDatabaseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (BaseConnection)d;
control.TypeOfDatabase = (DataBaseType)e.NewValue;
}
public DataBaseType TypeOfDatabase
{
get
{
return (DataBaseType)GetValue(TypeOfDatabaseProperty);
}
set
{
SetValue(TypeOfDatabaseProperty, value);
}
}
首先,我不建议您在 DPChanged 回调中更改依赖项 属性 的值。然后你在构造函数中所做的你必须在 DPChanged 回调中做。
这应该有效:
public DataBaseType TypeOfDatabase
{
get
{
return (DataBaseType)GetValue(TypeOfDatabaseProperty);
}
set
{
SetValue(TypeOfDatabaseProperty, value);
}
}
public static readonly DependencyProperty TypeOfDatabaseProperty = DependencyProperty.Register(nameof(TypeOfDatabase), typeof(DataBaseType), typeof(BaseConnection), new PropertyMetadata(DataBaseType.All, new PropertyChangedCallback(OnTypeOfDatabaseChanged)));
private static void OnTypeOfDatabaseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var connectionListByDatabaseType = new List<UcConnection>();
switch((DataBaseType)e.NewValue)
{
case DataBaseType.MySql:
foreach(UcConnection ucConnection in UcConnectionList)
{
if(ucConnection.DriverName.ToLower().Contains("mysql"))
{
connectionListByDatabaseType.Add(ucConnection);
}
}
UcConnectionList = connectionListByDatabaseType;
break;
case DataBaseType.SqlServer:
break;
default:
break;
}
}
您在设置 属性 之前在构造函数中打开 TypeOfDatabase
。
将 switch
语句移动到 OnTypeOfDatabaseChanged
回调或 Loaded
事件处理程序:
public BaseConnection()
{
InitializeComponent();
_languageCode = Thread.CurrentThread.CurrentCulture.Name;
this.Resources = _cultureHelper.FindRessourcesDictionnary(this.Resources, "UserControlsResources", _languageCode);
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
List<UcConnection> connectionListByDatabaseType = new List<UcConnection>();
switch (TypeOfDatabase)
{
case DataBaseType.MySql:
foreach (UcConnection ucConnection in UcConnectionList)
{
if (ucConnection.DriverName.ToLower().Contains("mysql"))
{
connectionListByDatabaseType.Add(ucConnection);
}
}
UcConnectionList = connectionListByDatabaseType;
break;
case DataBaseType.SqlServer:
break;
default:
break;
}
}