[英]Why does my Custom User Control Property value not match the value that was set in XAML?
正如标题所述,我有一个自定义用户控件。 我正在使用Silverlight 4,但我很确定这也适用于WPF。 我们的目标是创建一个控制,这将使选项的DEV只显示他们要显示一个特定页面上的控制部分,包含格式 , 排列一起, 控制替代文本 ,和定向性。
要显示的部分是:
Country
SubdivisionCategory
(州,区,边远地区) SubdivisionCensusRegion
(东北,南,中西部,西部) SubdivisionCensusDivision
(东北中环,东南中环等) Subdivision
我只是尝试访问DEV在XAML中创建此控件的实例时设置的属性的值。 我为每个属性设置了自定义DependencyProperty
对象,我认为这是为此目的。
我的问题是在SetMargins()
期间,所有this.ShowXxx
属性都等于DependencyProperty.Register
方法签名的new PropertyMetadata(true)
部分中设置的值。 因此,边距未正确设置。
如何获取this.ShowXxx
属性的初始XAML值或我在XAML中设置的任何其他属性?
以下是XAML标记的示例:
<local:CountryAndSubdivisionFilter
x:Name="ReportSettingsCountryAndSubdivisionFilter"
Orientation="Horizontal"
CountryFormat="Name"
SubdivisionFormat="Name"
ShowCountry="False"
ShowSubdivisionCategory="False"
ShowSubdivisionCensusRegion="False"
ShowSubdivisionCensusDivision="True"
ShowSubdivision="True"
SubdivisionCensusDivisionText="Region"
SubdivisionText="State"
Margin="0,15,0,0" />
以下是ShowCountry
属性的示例:
#region ShowCountry
public static readonly DependencyProperty ShowCountryProperty = DependencyProperty.Register
(
"ShowCountry",
typeof(bool),
typeof(CountryAndSubdivisionFilter),
new PropertyMetadata(true)
);
public bool ShowCountry
{
get
{
return (bool) GetValue(ShowCountryProperty);
}
set
{
SetValue(ShowCountryProperty, value);
this.CountryStackPanel.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
this.SetMargins();
}
}
#endregion
这是SetMargins()
方法:
private void SetMargins()
{
this.CountryStackPanel.Margin = new Thickness(0);
this.SubdivisionCategoryStackPanel.Margin = new Thickness(0);
this.SubdivisionCensusRegionStackPanel.Margin = new Thickness(0);
this.SubdivisionCensusDivisionStackPanel.Margin = new Thickness(0);
this.SubdivisionStackPanel.Margin = new Thickness(0);
var spList = new List<StackPanel>();
if (this.ShowCountry)
spList.Add(this.CountryStackPanel);
if (this.ShowSubdivisionCategory)
spList.Add(this.SubdivisionCategoryStackPanel);
if (this.ShowSubdivisionCensusRegion)
spList.Add(this.SubdivisionCensusRegionStackPanel);
if (this.ShowSubdivisionCensusDivision)
spList.Add(this.SubdivisionCensusDivisionStackPanel);
if (this.ShowSubdivision)
spList.Add(this.SubdivisionStackPanel);
int spIndex = 1;
foreach(var sp in spList)
{
var i = (spIndex < spList.Count) ? 15 : 0;
var j = (spIndex == 1) ? 0 : 15;
sp.Margin = (this.Orientation == Orientation.Horizontal)
? new Thickness(0, 0, i, 0)
: new Thickness(0, j, 0, 0);
spIndex++;
}
}
对于我上面的具体示例,我要做的是创建一个名为OnShowCountryPropertyChanged
的PropertyChangedCallback
然后从那里我可以将属性设置为NewValue
,它最初是在XAML中分配的值。
为了连接这个回调,我只需要更新我的DependencyProperty
的PropertyMetadata Constructor
,添加默认值,即对我的回调的引用。
旧签名: new PropertyMetadata(true)
新签名: new PropertyMetadata(true, OnShowCountryPropertyChanged)
一旦我做了这两件事,在我的SetMargins()
方法中访问属性的getter时就会使用XAML值。
#region ShowCountry
public static readonly DependencyProperty ShowCountryProperty = DependencyProperty.Register
(
"ShowCountry",
typeof(bool),
typeof(CountryAndSubdivisionFilter),
new PropertyMetadata(true, OnShowCountryPropertyChanged)
);
public bool ShowCountry
{
get
{
return (bool) GetValue(ShowCountryProperty);
}
set
{
SetValue(ShowCountryProperty, value);
this.CountryStackPanel.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
this.SetMargins();
}
}
private static void OnShowCountryPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctrl = d as CountryAndSubdivisionFilter;
ctrl.ShowCountry = (bool) e.NewValue;
}
#endregion
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.