当我调用此自定义控件时,我必须将属性值置于正确的顺序,因为第三个依赖项属性( ItemTypeIdCode )访问前两个的值( KeyField,ValueField )用于查找数据库中的数据,如果它们位于第一个属性之后,则它们的值为空。
<controls:DropDown x:Name="TheItemTypes"
KeyField="idCode" ValueField="title"
ItemTypeIdCode="itemTypes"
Width="150" HorizontalAlignment="Left" Margin="0 0 0 5"/>
如何才能使我的用户控件属性的顺序任意?,即在他们任何一个注册为更改之前,他们都先加载它们的值?
以下是我的用户控件的代码:
using System.Windows;
using System.Collections.Generic;
using TestApp.DataLayer;
namespace TestApp.Controls
{
public partial class DropDown : DependencyComboBox
{
#region DependencyProperty: ItemTypeIdCode
public string ItemTypeIdCode
{
get
{
if (GetValue(ItemTypeIdCodeProperty) != null)
return GetValue(ItemTypeIdCodeProperty).ToString();
else
return "";
}
set { SetValue(ItemTypeIdCodeProperty, value); }
}
public static readonly DependencyProperty ItemTypeIdCodeProperty =
DependencyProperty.Register("ItemTypeIdCode", typeof(string), typeof(DropDown),
new PropertyMetadata(string.Empty, OnItemTypeIdCodePropertyChanged));
private static void OnItemTypeIdCodePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
DropDown dropDown = dependencyObject as DropDown;
dropDown.OnPropertyChanged("ItemTypeIdCode");
dropDown.OnItemTypeIdCodePropertyChanged(e);
}
private void OnItemTypeIdCodePropertyChanged(DependencyPropertyChangedEventArgs e)
{
Items.Clear();
foreach (var kvp in Datasource.GetInstance().GetKeyValues(ItemTypeIdCode + "(" + KeyField + "," + ValueField + "); all; orderby displayOrder"))
{
Items.Add(new KeyValuePair<string, string>(kvp.Key, kvp.Value));
}
this.SelectedIndex = 0;
}
#endregion
#region DependencyProperty: KeyField
public string KeyField
{
get
{
if (GetValue(KeyFieldProperty) != null)
return GetValue(KeyFieldProperty).ToString();
else
return "";
}
set { SetValue(KeyFieldProperty, value); }
}
public static readonly DependencyProperty KeyFieldProperty =
DependencyProperty.Register("KeyField", typeof(string), typeof(DropDown),
new PropertyMetadata(string.Empty, OnKeyFieldPropertyChanged));
private static void OnKeyFieldPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
DropDown dropDown = dependencyObject as DropDown;
dropDown.OnPropertyChanged("KeyField");
dropDown.OnKeyFieldPropertyChanged(e);
}
private void OnKeyFieldPropertyChanged(DependencyPropertyChangedEventArgs e)
{
}
#endregion
#region DependencyProperty: ValueField
public string ValueField
{
get
{
if (GetValue(ValueFieldProperty) != null)
return GetValue(ValueFieldProperty).ToString();
else
return "";
}
set { SetValue(ValueFieldProperty, value); }
}
public static readonly DependencyProperty ValueFieldProperty =
DependencyProperty.Register("ValueField", typeof(string), typeof(DropDown),
new PropertyMetadata(string.Empty, OnValueFieldPropertyChanged));
private static void OnValueFieldPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
DropDown dropDown = dependencyObject as DropDown;
dropDown.OnPropertyChanged("ValueField");
dropDown.OnValueFieldPropertyChanged(e);
}
private void OnValueFieldPropertyChanged(DependencyPropertyChangedEventArgs e)
{
}
#endregion
public DropDown()
{
InitializeComponent();
DataContext = this;
}
}
}
答案 0 :(得分:1)
仅在设置了所有属性时初始化Items
:
private void InitItems()
{
if (!string.IsNullOrEmpty(ItemTypeIdCode) &&
!string.IsNullOrEmpty(KeyField) &&
!string.IsNullOrEmpty(ValueField))
{
Items.Clear();
foreach (var kvp in Datasource.GetInstance().GetKeyValues(ItemTypeIdCode + "(" + KeyField + "," + ValueField + "); all; orderby displayOrder"))
{
Items.Add(new KeyValuePair<string, string>(kvp.Key, kvp.Value));
}
this.SelectedIndex = 0;
}
}
private void OnItemTypeIdCodePropertyChanged(DependencyPropertyChangedEventArgs e)
{
InitItems();
}
private static void OnKeyFieldPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
DropDown dropDown = dependencyObject as DropDown;
dropDown.InitItems();
dropDown.OnPropertyChanged("KeyField");
dropDown.OnKeyFieldPropertyChanged(e);
}
private static void OnValueFieldPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
DropDown dropDown = dependencyObject as DropDown;
dropDown.InitItems();
dropDown.OnPropertyChanged("ValueField");
dropDown.OnValueFieldPropertyChanged(e);
}
答案 1 :(得分:0)
您是否可以在Load事件中执行设置处理而不是属性访问器?然后,所有需要的属性将由加载时间设置。