简体   繁体   English

使用typeconverter的datagrid的自定义数据源

[英]Custom datasource for datagrid with typeconverter

This is a followup on a question on RichTextBoxes in a grid . 这是关于网格中RichTextBoxes问题的后续操作。 I've gotten pretty far but it must be converted to MVVM now. 我已经走了很远,但是现在必须将其转换为MVVM。 My typeconverter is not getting called so the problem is probably in my databinding. 我的typeconverter没有被调用,所以问题可能出在我的数据绑定中。 I use two datagrids to test setups quicker. 我使用两个数据网格来更快地测试设置。

View gets a ViewModel that has the all the data. View获取具有所有数据的ViewModel。

<Window.Resources>
    <local:DifferenceToTextConverter  x:Key="DifferenceToTextConverter" />
    <DataTemplate x:Key="cellTemplate" DataType="{x:Type Label}">
        <Label Content="{Binding Converter={StaticResource ResourceKey=DifferenceToTextConverter}}" >
        </Label>
    </DataTemplate>
</Window.Resources>

        <DataGrid Name="TestGrid" 
                  ItemsSource="{Binding Source=DifferenceViewModel, Path=DifferenceData, Converter={StaticResource DifferenceToTextConverter}}" 
                  HeadersVisibility="Column"
                  ItemTemplate="{StaticResource cellTemplate}" >
        </DataGrid>
        </DataGrid>
        <DataGrid Name="OhterGrid" DataContext="{Binding ElementName=DifferenceViewModel, Path=DifferenceData}" HeadersVisibility="Column" >
            <DataGrid.ItemTemplate>
                <DataTemplate DataType="{x:Type Label}">
                    <Label Content="{Binding Converter={StaticResource ResourceKey=DifferenceToTextConverter}}" >
                    </Label>
                </DataTemplate>
            </DataGrid.ItemTemplate>
        </DataGrid>

    public DifferenceView(ViewModel.DifferenceViewModel differenceViewModel)
    {
        InitializeComponent();

        this.DifferenceViewModel = differenceViewModel;
    }

ViewModel, DataTable filled with objects of my custom class. ViewModel,DataTable充满了我的自定义类的对象。 I know this has data as the method to fill it get's called. 我知道这有数据作为调用它的方法。 And my converter, which sits in the project root namespace 还有我的转换器,它位于项目的根名称空间中

namespace ViewModel
{
    public class DifferenceViewModel
    {
        private DataTable differenceData;

        /// <summary>
        /// Differences between properties.
        /// </summary>
        public DataTable DifferenceData
        {
            get
            {
                return this.differenceData;
            }
            private set
            {
                this.differenceData = value;
            }
        }
    }
}

class DifferenceToTextConverter : System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type sourceType, object parameter, System.Globalization.CultureInfo culture)
    {
        TextBlock cell = new TextBlock();

        // Convert custom data to text representation.

        return cell;
    }
}

Constraints: 限制条件:

  1. Use MVVM 使用MVVM
  2. Style in xaml when possible. 尽可能在xaml中设置样式。
  3. Text with style applied to individual letters. 具有样式的文本应用于单个字母。
  4. Unknown number of columns and rows. 未知的列数和行数。
  5. Custom typeconverter needs to construct the entire cell text. 自定义类型转换器需要构造整个单元格文本。

The problem is your binding, you're not setting the DataContext correctly, no data is loaded and therefore your converter isn't being called. 问题是您的绑定,您没有正确设置DataContext,没有数据加载,因此没有调用您的转换器。

Make 2 changes: First, set your view's DataContext to the DifferenceViewModel : 进行2个更改:首先,将视图的DataContext设置为DifferenceViewModel

public DifferenceView(ViewModel.DifferenceViewModel differenceViewModel)
{       
   this.DataContext = differenceViewModel;
   InitializeComponent();
}

Then, change your binding: 然后,更改绑定:
Instead of this: 代替这个:

ItemsSource="{Binding Source=DifferenceViewModel, Path=DifferenceData, Converter={StaticResource DifferenceToTextConverter}}" 

Change it to this: 更改为此:

ItemsSource="{Binding DifferenceData, Converter={StaticResource DifferenceToTextConverter}}" 

Which basically means: 这基本上意味着:

ItemsSource="{Binding Path=DifferenceData, Converter={StaticResource DifferenceToTextConverter}}" 

Since your DataContext is the DifferenceViewModel, it'll directly go to the DifferenceData property. 由于您的DataContext是DifferenceViewModel,因此它将直接转到DifferenceData属性。 You can now put a breakpoint in your converter. 现在,您可以在转换器中放置一个断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM