正如标题所述,我有一个自定义用户控件。我正在使用Silverlight 4,但我很确定这也适用于WPF。目标是创建一个控件,为DEV提供选项 show 仅显示他们想要在特定页面上显示的控件部分,以及格式,对齐,控制备用文本和方向属性。
要显示的部分是:
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++;
}
}
答案 0 :(得分:4)
对于我上面的具体示例,我要做的是创建一个名为OnShowCountryPropertyChanged
的{{3}},然后我可以将该属性设置为NewValue
,最初是new PropertyMetadata(true)
在XAML中分配的值。
要连接此回调,我只需更新PropertyChangedCallback
的PropertyMetadata Constructor
即可添加默认值,即对我的回调的引用。
旧签名: new PropertyMetadata(true, OnShowCountryPropertyChanged)
新签名: SetMargins()
一旦我做了这两件事,当我的#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
方法访问了该属性的getter时,就会使用XAML值。
{{1}}