簡體   English   中英

為什么我的自定義用戶控件屬性值與XAML中設置的值不匹配?

[英]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++;
    }
}

回答


對於我上面的具體示例,我要做的是創建一個名為OnShowCountryPropertyChangedPropertyChangedCallback然后從那里我可以將屬性設置為NewValue ,它最初是在XAML中分配的值。

為了連接這個回調,我只需要更新我的DependencyPropertyPropertyMetadata 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM