如何值的同一集合捆绑为一个DataGridComboBox所有的组合框? [英] How to bind the same collection of values into all ComboBoxes in a DataGridComboBox?

查看:104
本文介绍了如何值的同一集合捆绑为一个DataGridComboBox所有的组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有值的集合在一个字符串数组,或在列表或什么的,我想要的是值项的集合的集合设置成一列DataGridComboBoxColumn在WPF所有的组合框。

我觉得我没有其他的方式来访问值的集合,同样其绑定到所有的组合框(在XAML)DataContext的旁边。但是我可以访问一个DataGrid的DataContext的从DatGridComboBoxColumn(在XAML)?我可以做吗?怎么样?

我如何指定(XAML)在DatagridComboBoxColumn把项目的集合同样在所有的组合框?我怎样才能做到这一点?

下面是我的XAML:

 (...)的xmlns:地方=CLR的命名空间:WpfApp(......)<电网NAME =GRID1>
    < D​​ataGrid的名称=的数据网格的AutoGenerateColumns =真>
        < D​​ataGrid.Columns>
            < D​​ataGridComboBoxColumn标题=选项WIDTH =100/>
        < /DataGrid.Columns>
    < / DataGrid的>
< /网格和GT;

这是我的code背后:

 公共类识别TestClass
{
    私人静态字符串[]字符串数组= {方案一,方案二,方案三};    公共静态字符串[]字符串数组
    {
        得到
        {
            返回字符串数组;
        }
    }
}


解决方案

  

但我可以从DatGridComboBox列(在XAML)访问DataGrid的DataContext的?我可以做吗?怎么样?


有是访问的的DataContext 的DataGrid 从列定义,因为它是不是没有简单的方法视觉或逻辑树的一部分,因此它不会继承DataContext的。我最近在博客关于<一个href=\"http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/\"相对=nofollow>解决这个问题。基本上你需要做这样的事情:

 &LT; D​​ataGrid的名称=的数据网格的AutoGenerateColumns =真&GT;
    &LT; D​​ataGrid.Resources&GT;
        &LT;! - 代理当前的DataContext - &GT;
        &lt;局部:BindingProxy X:键=代理数据={结合} /&GT;
    &LT; /DataGrid.Resources>
    &LT; D​​ataGrid.Columns&GT;
        &LT; D​​ataGridComboBoxColumn标题=选项WIDTH =100
                                的ItemsSource ={绑定源={StaticResource的代理},路径=数据}/&GT;
    &LT; /DataGrid.Columns>
&LT; / DataGrid的&GT;

BindingProxy 是一个简单的类,从可冻结派生,并暴露了一个数据依赖属性。

I have a Collection of Values in an Array of Strings, or in a List or whatever, and what i want is to set this Collection of values as the Collection of items into all the ComboBoxes in a column as DataGridComboBoxColumn in WPF.

I think i don't have other way to access this Collection of Values and bind it equally to all the ComboBoxes (in XAML) beside the DataContext. But can i access the DataContext of a DataGrid from a DatGridComboBoxColumn (in XAML)? Can i do it? How?

How i specify (in XAML) in DatagridComboBoxColumn to put this Collection of Items equally in all ComboBoxes? How i can achieve this?

Here is my XAML:

(...)xmlns:local="clr-namespace:WpfApp"(...)

<Grid Name="grid1">
    <DataGrid Name="dataGrid" AutoGenerateColumns="True">
        <DataGrid.Columns>
            <DataGridComboBoxColumn Header="Options" Width="100"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

And here is my Code Behind:

public class TestClass
{
    private static string[] stringArray = { "Option One", "Option Two", "Option Three" };

    public static string[] StringArray
    {
        get
        {
            return stringArray;
        }
    }
}

解决方案

But can i access the DataContext of a DataGrid from a DatGridComboBox Column (in XAML)? Can i do it? How?

There's no straightforward way to access the DataContext of the DataGrid from the column definition, because it isn't part of the visual or logical tree, so it doesn't inherit the DataContext. I recently blogged about a solution to that issue. Basically you need to do something like that:

<DataGrid Name="dataGrid" AutoGenerateColumns="True">
    <DataGrid.Resources>
        <!-- Proxy for the current DataContext -->
        <local:BindingProxy x:Key="proxy" Data="{Binding"} />
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridComboBoxColumn Header="Options" Width="100"
                                ItemsSource="{Binding Source="{StaticResource proxy}", Path=Data}"/>
    </DataGrid.Columns>
</DataGrid>

BindingProxy is a simple class that derives from Freezable and exposes a Data dependency property.

这篇关于如何值的同一集合捆绑为一个DataGridComboBox所有的组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆