相关疑难解决方法(0)

如何将WPF DataGrid绑定到可变数量的列?

我的WPF应用程序生成的数据集可能每次都有不同的列数.输出中包含将用于应用格式的每列的说明.输出的简化版本可能类似于:

class Data
{
    IList<ColumnDescription> ColumnDescriptions { get; set; }
    string[][] Rows { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

此类在WPF DataGrid上设置为DataContext,但实际上我以编程方式创建列:

for (int i = 0; i < data.ColumnDescriptions.Count; i++)
{
    dataGrid.Columns.Add(new DataGridTextColumn
    {
        Header = data.ColumnDescriptions[i].Name,
        Binding = new Binding(string.Format("[{0}]", i))
    });
}
Run Code Online (Sandbox Code Playgroud)

有没有办法用XAML文件中的数据绑定替换此代码?

c# data-binding wpf xaml datagrid

121
推荐指数
4
解决办法
11万
查看次数

单元测试附加行为wpf

一般来说,我仍然习惯于附加行为,并且看不出如何为一个人编写单元测试.

我在Sacha Barber的Cinch框架下面粘贴了一些代码,允许通过附加行为关闭窗口.有人能告诉我一个示例单元测试吗?

谢谢!
Berryl

    #region Close

    /// <summary>Dependency property which holds the ICommand for the Close event</summary>
    public static readonly DependencyProperty CloseProperty =
        DependencyProperty.RegisterAttached("Close",
            typeof(ICommand), typeof(Lifetime),
                new UIPropertyMetadata(null, OnCloseEventInfoChanged));

    /// <summary>Attached Property getter to retrieve the CloseProperty ICommand</summary>
    public static ICommand GetClose(DependencyObject source)
    {
        return (ICommand)source.GetValue(CloseProperty);
    }

    /// <summary>Attached Property setter to change the CloseProperty ICommand</summary>
    public static void SetClose(DependencyObject source, ICommand command)
    {
        source.SetValue(CloseProperty, command);
    }

    /// <summary>This is the property changed handler for the Close property.</summary>
    private …
Run Code Online (Sandbox Code Playgroud)

wpf unit-testing attachedbehaviors

7
推荐指数
1
解决办法
3418
查看次数