【问题标题】:Find DataGrid for DataGridColumn为 DataGridColumn 查找 DataGrid
【发布时间】:2011-03-10 19:09:37
【问题描述】:

如何获取给定DataGridColumnDataGrid。我在DataGrid 的子类中创建了一个附加属性,该属性适用于DataGridColumn(我没有对其进行子类化)。它给了我 DataGridColumn 我在其上应用附加属性,但我如何获得 DataGrid 引用本身?我两个都需要。

编辑:

我更感兴趣的是,如何在附加属性的事件处理程序中获取实际托管附加属性的DependencyObject 实例。即DataGrid,而不是DataGridColumn,该属性也被附加。

<my:MvvmDataGrid x:Name="_dataGrid" ... >
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn my:MvvmDataGrid.SingleClickEdit="true" .../>
    </sdk:DataGrid.Columns>
</my:MvvmDataGrid>

然后我有一个静态处理程序OnSingleClickEditPropertyChanged,它在SingleClickEditProperty 附加属性的元数据上注册为PropertyChangedCallback

当该属性调用时(id est 列上的属性已更改),当我需要 MvvmDataGrid 实例时,我会得到一个 DataGridTextColumn

【问题讨论】:

    标签: silverlight attached-properties


    【解决方案1】:

    我觉得你可以用this

    使用此代码,您可以在可视树中找到 DataGridColumn 的祖先 - 您的 DataGrid。此代码实现为静态函数,但您可以将其更改为具有更多“说话”名称的扩展方法,例如 FindAncestor:

    public static class UIElementExtensions
    
    {
    
        public static T FindAncestor<T>(this UIElement control) where T: UIElement  
        {
    
            UIElement p = VisualTreeHelper.GetParent(control) as UIElement;
            if (p != null)
            {
                if (p is T)
                    return p as T;
                else
                    return p.FindAncestor<T>();
            }
            return null;
        }
    }
    

    并使用它:

    DataGrid p = dataGridColumn.FindAncestor< DataGrid >();
    

    如果您需要从 XAML 获取 DataGrid,请尝试使用来自 this article 的绑定。

    祝你好运。

    更新

    我明白这是怎么回事。下一个答案不会那么容易,但它是silverlight :) 那么,为什么使用 VisualTreeHelper 无法从 DataGridColumn 中找到 DataGrid?因为,可视树中不存在 DataGridColumn。 DataGridColumn 继承自 DependencyObject,而不是 UIElement。 忘记VisualTree,新的idea会是这样的:我们在DataGridColumn中添加附加属性——命名为Owner,并将DataGrid绑定到这个属性。 但是,DataGridColumn 是 DependencyObject 并且 ElementName 的任何绑定在 silverlight 4 中都不起作用。 我们只能绑定到 StaticResource。所以,去做吧。 1) DataGridColumn 的所有者附加属性:

    public class DataGridHelper
    {
        public static readonly DependencyProperty OwnerProperty = DependencyProperty.RegisterAttached(
                "Owner",
                typeof(DataGrid),
                typeof(DataGridHelper),    
                        null));
    
        public static void SetOwner(DependencyObject obj, DataGrid tabStop)
        {
            obj.SetValue(OwnerProperty, tabStop);
        }
    
        public static DataGrid GetOwner(DependencyObject obj)
        {
            return (DataGrid)obj.GetValue(OwnerProperty);
        }
    }
    

    2) DataGrid Xaml(例如):

    <Controls:DataGrid x:Name="dg" ItemsSource="{Binding}">
        <Controls:DataGrid.Columns>
            <Controls:DataGridTextColumn  h:DataGridHelper.Owner="[BINDING]"/>
        </Controls:DataGrid.Columns>
    </Controls:DataGrid>
    

    3) DataGrid Container - StaticResource 中 DataGrid 实例的守护者:

    public class DataGridContainer : DependencyObject
    {
    
        public static readonly DependencyProperty ItemProperty =
    DependencyProperty.Register(
    "Item", typeof(DataGrid),
    typeof(DataGridContainer), null
    );
    
        public DataGrid Item
        {
            get { return (DataGrid)GetValue(ItemProperty); }
            set { SetValue(ItemProperty, value); }
        }
    }
    

    4) 添加到 DataGridContainer 视图实例的资源中,并将 DataGrid 实例绑定到 Item 属性:

    <c:DataGridContainer x:Key="ownerContainer" Item="{Binding ElementName=dg}"/> 
    

    这里可以通过 ElementName 进行绑定。

    5) 最后一步,我们将 DataGrid 绑定到附加属性 Owner(参见第 2 页并将下一个代码添加到 [BINDING] 部分):

    {Binding Source={StaticResource ownerContainer}, Path=Item}
    

    就是这样。在代码中,如果我们引用了 DataGridColumn,我们可以获得拥有的 DataGrid:

    DataGrid owner = (DataGrid)dataGridColumn.GetValue(DataGridHelper.OwnerProperty);** 
    

    希望这个原则对你有所帮助。

    【讨论】:

    • 我想到了这个,但是VisualTreeHelper.GetParent在我尝试获取DataGridColumn的父级时抛出异常。
    • 第 2 步中的“[BINDING]”到底是什么意思。您的意思是“{Binding}”吗?如果是这样,那将无济于事。
    • 见第 5 步。我写希望你应该在这个地方添加
    猜你喜欢
    • 1970-01-01
    • 2011-05-29
    • 2011-11-08
    • 2014-08-29
    • 2013-08-01
    • 2010-10-14
    • 2011-11-02
    • 1970-01-01
    • 2012-10-04
    相关资源
    最近更新 更多