[英]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.