When I call this custom control, I have to put the attribute values in the correct order since the third dependency property (ItemTypeIdCode) accesses the values of the first two (KeyField, ValueField) to look up data in the database, and if they come after the first attribute, then their values are empty.
<controls:DropDown x:Name="TheItemTypes"
KeyField="idCode" ValueField="title"
ItemTypeIdCode="itemTypes"
Width="150" HorizontalAlignment="Left" Margin="0 0 0 5"/>
How can I make the order of my user control attributes arbitrary? i.e. so that they all load their values first before any of them register as changed?
Here is the code for my user control:
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) != n开发者_Go百科ull)
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;
}
}
}
Initialize Items
only when all properties are set :
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);
}
Any way you can do the setup processing in the Load event instead of the property accessors? Then all the needed properties will be set by load time.
精彩评论