WPF XAML尝试绑定DataGrid列的宽度

时间:2017-03-16 08:39:45

标签: wpf xaml binding datagrid width

我想将我的列的宽度绑定到我的模型中的属性,以便在用户调整大小时保存它。我想要一个没有代码的解决方案。 这就是我到目前为止所做的:

XAML:

<DataGrid x:Name="dgArticles"  AutoGenerateColumns="False" ItemsSource="{Binding Specifications.Articles}" RowDetailsVisibilityMode="Visible">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="Number" Header="Number" Binding="{Binding Number}" Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.Specifications.Config.NumberColumnWidth}" MinWidth="70" >

型号:

public class Specifications
{
    private ConfigurationGrid config
    public ConfigurationGrid Config { get { return config; } set { } }

    private ObservableCollection<Article> articles;
    public ObservableCollection<Article> Articles
    {
        get { return articles; }
        set { }
    }


public class ConfigurationGrid : INotifyPropertyChanged
{
    private double numberColumnWidth;
    public double NumberColumnWidth
    {
        get { return numberColumnWidth; }
        set { numberColumnWidth = value;  OnPropertyChanged("numberColumnWidth"); }
    }

    public ConfigurationGrid() { }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this,
                new System.ComponentModel.PropertyChangedEventArgs(propertyName));
    }
}

我设法将我的RowDetailsTemplate中的子Datagrid列的宽度绑定到另一列的宽度,如下所示:

<DataGridTextColumn Header="Quantity" CellStyle="{StaticResource QuantityStyle}" Binding="{Binding Quantity, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:n\}}" 
                                                    Width="{Binding Source={x:Reference Mesure}, Path=ActualWidth}"/>

这很好但我不知道它为什么不在我的主DataGrid上工作。 经过调试后,我注意到它甚至没有达到NumberColumnWidth的Getter。 有没有人知道如何使它工作?谢谢

修改

我尝试了@ mm8提供的解决方案,但它没有用。它还没有到达Getter。也许我错过了什么。以下是代码现在的样子:

XAML:

<UserControl x:Class="CachView.Views.GridView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         xmlns:local="clr-namespace:CachView.ViewModels"
         xmlns:conv="clr-namespace:CachView.Converters"
         xmlns:util="clr-namespace:CachView.Util"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

<UserControl.DataContext>
    <local:ArticleViewModel/>
</UserControl.DataContext>

<Grid Margin="10">
    <DataGrid x:Name="dgArticles"  AutoGenerateColumns="False" ItemsSource="{Binding Specifications.Articles}" RowDetailsVisibilityMode="Visible">
        <DataGrid.Resources>
            <util:BindingProxy x:Key="proxy" Data="{Binding}"/>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="Number" Header="Number" Binding="{Binding Number}" Width="{Binding Data.Specifications.Config.NumberColumnWidth, Source={StaticResource proxy}}"
                                MinWidth="70">

            </DataGridTextColumn>

代码背后:

 public partial class GridView : UserControl
{
    public GridView(ArticleViewModel a)
    {            
        InitializeComponent();
        this.DataContext = a;
    }   
}

我的BindingProxy类与示例中的相同:

    class BindingProxy : Freezable
{
    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }


    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}

我的项目是一个UserControl,用于从WinForm应用程序中使用。以下是它的实现方式以及我如何设置DataContext和属性。它是从我的WinForm应用程序的控制器完成的。

class Controller
{

    private ArticleViewModel articleViewModel;
    private ElementHost elementHost;
    private MainWindow winformView;
    public ArticleViewModel ArticleViewModel { get { return articleViewModel; } }
    public Collection<Article> Articles { get; set; } 
    public Specifications Specs { get; set; }       

    public Controleur(MainWindow view) // The view is received from Program.cs
    {
        this.winformView = view;
        Articles = new Collection<Article>();
        populateArticles(); // This create hard coded articles for testing purpose

        ConfigurationGrid config= new ConfigurationGrid();
        config.NumberColumnWidth = 300;

        Specs = new Specifications(Articles);
        Specs.Config = config;

        articleViewModel = new ArticleViewModel(Specs);


        GridView gridView = new GridView(articleViewModel); //This is my WPF UserControl

        elementHost = new ElementHost();
        elementHost.Dock = DockStyle.Fill;
        this.winformView.Controls.Add(elementHost);
        elementHost.Child = gridView;           
    }

我的ViewModel:

public class ArticleViewModel 
{
    private Specifications specifications;
    public Specifications Specifications { get { return specifications; } set { } }



    public ArticleViewModel() { }

    public ArticleViewModel(Specifications c)
    {
        this.specifications = c;
    }
}

欢迎任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

DataGridTextColumn不是添加到元素树的可视元素,因此您无法绑定到RelativeSource,因为没有要绑定的祖先。

如果您希望能够将Width属性绑定到视图模型属性,则可以使用BindingProxy对象捕获DataContext,如以下博客文章中所建议的那样。 / p>

[WPF]如何在未继承DataContext时绑定到数据: https://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/