编程语言
首页 > 编程语言> > c# – 为什么我的自定义用户控件属性值与XAML中设置的值不匹配?

c# – 为什么我的自定义用户控件属性值与XAML中设置的值不匹配?

作者:互联网

背景

正如标题所述,我有一个自定义用户控件.我正在使用Silverlight 4,但我很确定这也适用于WPF.目标是创建一个控件,为DEV提供选项,以便仅显示他们想要在特定页面上显示的控件部分,以及格式,对齐,控制替代文本和方向属性.

要显示的部分是:

>国家
>细分类别(州,区,边远地区)
> SubdivisionCensusRegion(东北,南,中西部,西部)
> SubdivisionCensusDivision(东北中环,东南中环等)
>细分

我只是尝试访问DEV在XAML中创建此控件的实例时设置的属性的值.我为每个属性设置了自定义DependencyProperty对象,我认为这是为此目的.

问题

我的问题是在SetMargins()期间,所有this.ShowXxx属性都等于在DependencyProperty.Register方法签名的新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中分配的值.

为了连接这个回调,我只需要更新我的DependencyPropertyPropertyMetadata Constructor添加到默认值,即对我的回调的引用.

旧签名:新的PropertyMetadata(true)

新签名:新的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

标签:c,wpf,silverlight,silverlight-4-0
来源: https://codeday.me/bug/20190624/1281642.html