【问题标题】:Creating property for WPF user control datagrid为 WPF 用户控件数据网格创建属性
【发布时间】:2016-06-14 06:16:51
【问题描述】:

我有一个带有名为 IGrid 的数据网格的用户控件。我想为它添加 GridViewColumnCollection 属性。

public class DataGridNumericColumn : DataGridTextColumn
{
    protected override object PrepareCellForEdit(System.Windows.FrameworkElement editingElement, System.Windows.RoutedEventArgs editingEventArgs)
    {
        TextBox edit = editingElement as TextBox;
        edit.PreviewTextInput += OnPreviewTextInput;
        return base.PrepareCellForEdit(editingElement, editingEventArgs);
    }
    void OnPreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
    {
        try
        {
            Convert.ToInt32(e.Text);
        }
        catch
        {
            e.Handled = true;
        }
    }
}

我从谷歌得到了一段代码`

private Collection<DataGridColumn> field = new Collection<DataGridColumn>();
[Category("Data")]
[Description("Column Creation")]                     
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Collection<DataGridColumn> Columns
{
    get { return field; }
}

在这里我可以在可视化树中获取 GridViewColumnCollection , 我的问题是如何使用上面的代码在集合中添加一个新类型(DataGridNumericColumn)。

【问题讨论】:

  • 请任何人回复

标签: wpf user-controls


【解决方案1】:

如果您不知道代码的作用,最好在尝试使用它或提出自己的解决方案之前对其进行研究。据我所知,您正在尝试向继承 DataGrid 的 UserControl 添加一个附加属性。

你有两个选择:

  1. 创建一个 DependencyProperty(我认为最好的选择)。
  2. 创建普通属性

这里有几个链接可以帮助您入门:

What is the difference between Property and Dependency Property

When should I use dependency properties in WPF?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-29
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    相关资源
    最近更新 更多