簡體   English   中英

子級不繼承依賴項屬性值,而采用默認值

[英]Child doesn't inherit dependency property value and takes default value

我有一個依賴項屬性,該屬性應從附加屬性繼承其值,但永遠不會。 它似乎總是采用其默認值。

我的設定

我有一個從Microsoft的DataGrid繼承的自定義網格和一個從Microsoft的DataGridTextColumn繼承的CustomColumn。

我的CustomGrid具有帶有Inherits標志的可繼承的附加屬性“ ShowToolTip”,而我的CustomColumn具有“ ShowToolTip”依賴項屬性。

CustomColumn“ ShowToolTip”從不接受在我的CustomGrid中設置為ShowToolTip的值。

CustomGrid附加屬性:

public class CustomGrid: DataGrid
{
        static CustomGrid()
        {
             ShowToolTipProperty = DependencyProperty.RegisterAttached("ShowToolTip", typeof(bool), typeof(CustomGrid), 
                new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));

        }

        public static bool GetShowToolTip(DependencyObject target)
        {
            return (bool)target.GetValue(ShowToolTipProperty);
        }

        public static void SetShowToolTip(DependencyObject target, bool value)
        {
            target.SetValue(ShowToolTipProperty, value);
        }

        public bool ShowToolTip
        {
            get
            {
                return GetShowToolTip(this);
            }
            set
            {
                SetShowToolTip(this, value);
            }
        }
}

CustomColumn屬性:

public class CustomColumn : DataGridTextColumn
{
        static CustomColumn ()
        {
              ShowToolTipProperty = CustomGrid.ShowToolTipProperty.AddOwner(typeof(CustomColumn ), 
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));
        }

        public bool ShowToolTip
        {
           get { return (bool)GetValue(ShowToolTipProperty); }
           set { SetValue(ShowToolTipProperty, value); }
        }
}

在Xaml中使用 (僅綁定相關屬性)

<myNamespace:CustomGrid Name="myGrid"  ShowToolTip="False">
    <DataGrid.Columns>
        <myNamespace:CustomColumn Binding="{Binding Value1}" ShowToolTip="True"/>
        <myNamespace:CustomColumn Binding="{Binding Value2}" ShowToolTip="False"/>
        <myNamespace:CustomColumn Binding="{Binding Value3}" /> 
    </DataGrid.Columns>
</myNamespace:CustomGrid >

在此示例中,我期望:

  • Column1(Value1):應該顯示工具提示
  • Column2(Value2):不應顯示工具提示
  • Column3(Value3):不應顯示工具提示

我得到的是:

  • 列1(值1):顯示工具提示
  • Column2(Value2):不顯示工具提示
  • Column3(Value3): SHOWS工具提示

似乎它采用的是CustomColumn類中屬性的默認值。

我嘗試更改CustomColumn.ShowToolTip屬性與CustomGrid.ShowToolTip屬性的關聯,但是我做什么都沒關系,它從不繼承自CustomGrid的值。

ShowToolTipProperty = CustomGrid.ShowToolTipProperty.AddOwner(typeof(CustomColumn ), 
                new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));

我該怎么做才能使CustomColumn.ShowToolTip屬性繼承自CustomGrid.ShowToolTip屬性的值?

DataGridColumn不在VisualGrid中的DataGrid下。 這是一個普遍的誤解。

如這里所示:

在此處輸入圖片說明

您通過DataGrid設置並影響列的所有Dependency屬性都不會被繼承,而是在DataGrid的代碼中完成。

例如:DataGrid的CanUserResizeColumns屬性,該屬性設置DataGridColumn的CanUserResize屬性。 您是否看過源代碼以獲取有關其完成方式的示例。

您還可以看到DataGrid的Columns存儲在Property中,而不是作為DataGrid的內容存儲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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