English 中文(简体)
如何从外部设置 DataGrid s 数据源?
原标题:How to set the DataGrid s DataSource from outside?

我在WPF开发一个控制器,我的控制器包含一些其他的WPF控制器,比如标签、按钮和一个数据Grid。所以,我一直试图做的是创建一个依赖属性属性,允许我从我的容器控制器外部获取数据源。

到目前为止,我有这个密码:

public static readonly DependencyProperty dataSourceProperty = DependencyProperty.Register(
                                                                            "DataSource",
                                                                            typeof(object),
                                                                            typeof(MyCustomControl));
    public object DataSource
    {
        get
        {
            return (object)GetValue(dataSourceProperty);
        }
        set
        {
            SetValue(dataSourceProperty, value);
        }
    }

但我无法理解的是 我要写出这个代码的地方:

    myDataGrid.ItemsSource = DataSource;

我的意思是,我需要的是,在我的Xaml档案中, 能够做到这一点:

<MyCustomControl Name="MyControl" DataSource={Binding MyData}/>

我希望你能帮我 提前谢谢你

最佳回答

经过几个小时的研究,我找到了解决我问题的办法:

既然我需要从集装箱控制系统 获得一个嵌套的附属控制财产 我就这么做了:

public static readonly DependencyProperty itemsSourceProperty = ItemsControl.ItemsSourceProperty.AddOwner(
                                                                        typeof(MyCustomControl), 
                                                                        new FrameworkPropertyMetadata(
                                                                            ItemsSourcePropertyChangedCallback));

    public System.Collections.IEnumerable ItemsSource
    {
        get
        { return (System.Collections.IEnumerable)GetValue(itemsSourceProperty); }

        set
        { SetValue(itemsSourceProperty, value); }
    }

    private static void ItemsSourcePropertyChangedCallback(DependencyObject controlInstance, DependencyPropertyChangedEventArgs args)
    {
        MyCustomControlraceUIGridControlInstance = (MyCustomControl)controlInstance;  
        raceUIGridControlInstance.extendedDataGrid.ItemsSource = (System.Collections.IEnumerable)args.NewValue;            
    }

完成此任务后,您可以写入 XAML :

<MyCustomControl Name=mycustomControl ItemsSource="{Binding YourData}" />

您可以使用任何嵌巢控制附属属性来做到这一点 。

问题回答

假设当前表格的数据变量有 MyData 属性, 您只需要以下信息:

<MyCustomControl Name="MyControl" DataContext={Binding MyData}/>

在您的自制控制中,您可以使用下列方式与 MyData 绑定:

<DataGrid ItemsSource = "{Binding}"/>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签