在datagrid中使用的通用控件 [英] Universal control to use in datagrid

查看:77
本文介绍了在datagrid中使用的通用控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我有以下要求:我有一个对象实例的列表,其中每个实例都具有我在运行时不知道的类型的属性(例如DateTime,string或int).我想用该列表填充一个数据网格,对于上述属性,在选择单元格时显示适当的编辑器.

我正在尝试通过自定义控件来实现此目的,如下面的代码所示:

Hello!

I have the following requirement: I have a list of object instances where each instance has a property of a type I do not know at runtime (e.g. DateTime, string or int). I want to fill a datagrid with that list and for the afore mentioned property, show the appropriate editor when the cell is selected.

I am trying to accomplish this with a custom control like the code shows below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DataGridExample
{
    public class UniversalControl : ContentControl
    {
        public UniversalControl()
        {
            this.DefaultStyleKey = typeof(UniversalControl);
        }

        public object Value
        {
            get
            {
                return this.GetValue(ValueProperty);
            }
            set
            {
                this.SetValue(ValueProperty, value);
            }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register(
                "Value",
                typeof(object),
                typeof(UniversalControl),
                new PropertyMetadata(null, ValuePropertyChanged));

        private static void ValuePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
        {
            DatePicker picker = new DatePicker();
            if (eventArgs.NewValue != null && eventArgs.NewValue is DateTime)
                picker.SelectedDate = (DateTime)eventArgs.NewValue;

            ((UniversalControl)sender).Content = picker;
            ((UniversalControl)sender).Visibility = Visibility.Visible;
        }

        public override void OnApplyTemplate()
        {
             base.OnApplyTemplate();
        }
    }
}



因此,在值更改的处理程序中,我创建了与绑定值一起使用的控件(在此示例中,仅包含日期选择器,但这与想法有关).

无论如何,结果是我什么也看不见,调试器在处理程序中停止,并且我看到创建控件的代码正在执行.

任何人都知道这可能是什么吗?

问候,Perry



So, in the valuechanged handler I create the control which goes with the bound value (in this example only the datepicker, but it is about the idea).

Anyway, the result is that I do not see anything happening, while the debugger stops in the handler and I can see the code creating the control being executed.

Anyone has an idea what this could be?

Regards, Perry

推荐答案

您可以对列使用CellTemplate(属性名称应相似)来完成此操作.
用DataTriggers编写DataTemplate.这些DataTrigger有助于显示和隐藏不同的控件.


让我们解决您的问题:
您是否检查过ValueChanged处理程序是否始终执行(应该等于列表中的项目数)?

我注意到的事情:
即使数据不是DateTime,它也会显示控件.应该像下面的
You can accomplish this using CellTemplate (the property name should be similar) for the Column.

Write a DataTemplate with DataTriggers. These DataTrigger would help in showing and hiding the different controls.


Lets go to your problem:
Did you checked whether the ValueChanged handler executes all the times (it should be equal to the number of items in the list)?

Thing I noticed:
Even the data is not DateTime it would show the control. It should be like below
if (eventArgs.NewValue != null && eventArgs.NewValue is DateTime)
{
    picker.SelectedDate = (DateTime)eventArgs.NewValue;
    ((UniversalControl)sender).Content = picker;
    ((UniversalControl)sender).Visibility = Visibility.Visible;
}



如果eventArgs.NewValuenull,则以下代码可能会引发运行时错误.检查!= null
时避免使用&&运算符



The below code might throw runtime error if the eventArgs.NewValue is null. Avoid using && operator while checking for != null

if (eventArgs.NewValue != null && eventArgs.NewValue is DateTime)



建议:
不必每次都强制转换UniversalControl,您可以在方法的开头定义一个变量并开始使用它.

更新:
UniversalControl样式没有任何ContentPresenter来显示您从背后的代码动态添加的内容.

它应该如下所示:



Suggestion:
Instead of casting UniversalControl everytime you can define a variable at the beginning of the method and start using it.

Update:
UniversalControl style doesn''t have any ContentPresenter to show the Content you add dynamically from the codebehind.

It should be like below:

<Style TargetType="local:UniversalControl">
        <setter property="Template">
            <setter.value>
                <controltemplate targettype="local:UniversalControl">
                    <border removed="{TemplateBinding Background}">
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <contentpresenter />
                    </border>
                </controltemplate>
            </setter.value>
        </setter>
    </Style>



看,我添加了ContentPresenter来显示内容. :)

如果有帮助,请将其标记为答案".



See, I have added a ContentPresenter to show the Content. :)

Mark it as Answer if it is helpful.


这篇关于在datagrid中使用的通用控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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