WPF:如何将'System.Collections.Generic.Dictionary< string,object>'转换为'System.Windows.Controls.DataGridRow' [英] WPF: How to convert 'System.Collections.Generic.Dictionary<string,object>' to 'System.Windows.Controls.DataGridRow'

查看:376
本文介绍了WPF:如何将'System.Collections.Generic.Dictionary< string,object>'转换为'System.Windows.Controls.DataGridRow'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagrid,我将使用" ObservableCollection< Dictionary< string,object>>绑定行。 dataRows {get;组; }"

I have a datagrid, i will be binding rows using "ObservableCollection<Dictionary<string, object>> dataRows { get; set; }"

datagrid.itemssource = datarows;

datagrid.itemssource = datarows;

当我尝试访问一个项目时,我收到它作为带有Dictionary的可观察集合。当我将项目传递给下面的步骤时,我接收行为空。

When I try to access an item I am receiving it as observable collection with Dictionary. When I pass the item to the below step I am receiving row as null.

foreach(myDataGrid.Items中的var项目)

foreach(var item in myDataGrid.Items)

{

DataGridRow row =(DataGridRow)scni.DataGridInstance.DataGridControl.ItemContainerGenerator.ContainerFromItem(item);

DataGridRow row = (DataGridRow)scni.DataGridInstance.DataGridControl.ItemContainerGenerator.ContainerFromItem(item);

}

我认为我们传递该项目的方式不正确。我不知道,我怎么能通过正确的项目来准确地从他们的单元格中获取行。 

I thought the way we are passing the item is not in proper format. I don't know, how can I pass proper item to get row exactly and from their cell. 

因此,我试图转换"字典"到" Datagridrow "我需要访问Cell值及其属性。

Hence, I am trying to convert "dictionary" to "Datagridrow" where I need to access Cell value and its properties.

我的最终目标是从datagrid获取每个单元格值并更改单元格属性(如前景/背景/文本对齐等)基于单元格值。

My final aim is to get each and every cell value from the datagrid and change cell properties (like foreground/Background/Text Allignment etc) based on the cell value.

注意:我无法从XAML端执行此操作。我需要通过改变c#代码来做一切事情。

Note: I cannot do it from the XAML side. I need to do everything programitically by doing changes in c# code

请帮我解决这个问题。

提前致谢

推荐答案

嗨aviapt,

Hi aviapt,

根据你的描述,你想使用字典bing datagrid,是或否?如果是,你可以像observablecollection那样做。

According to your description, you want to bing datagrid using dictionary, yes or not? If yes, you can do this like observablecollection.

 <StackPanel>
        <DataGrid
           AutoGenerateColumns="False" 
            Width="300"
            Height="300"
            ItemsSource="{Binding}">
            <DataGrid.Columns>
                <DataGridTextColumn
                    Width="50"
                    Binding="{Binding Key}"
                    Header="Key" />
                <DataGridTextColumn
                    Width="50"
                    Binding="{Binding Value}"
                    Header="Value" />
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>


 Dictionary<int, string> dic;
        public Window4()
        {
            InitializeComponent();
            dic = new Dictionary<int, string>();
          
            dic.Add(1, "Cherry");
            dic.Add(2, "Barry");
            dic.Add(3, "Mattew");
            this.DataContext = dic;                           
        }

您可以按单元格值更改单元格属性。

The you can change cell property by cell value.

最好的问候,

Cherry


这篇关于WPF:如何将'System.Collections.Generic.Dictionary&lt; string,object&gt;'转换为'System.Windows.Controls.DataGridRow'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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