c# - 获取DataGrid宽度

标签 c# wpf binding datagrid

我的问题是我在数据网格中填充了如下代码:

(...)
                while (rdr.Read())
                {
                    dataGrid1.Items.Add(new Produkt { nazwa = rdr.GetString(rdr.GetOrdinal("nazwa")), cena = rdr.GetString(rdr.GetOrdinal("cena")), kod = rdr.GetString(rdr.GetOrdinal("kod")) });

                }
(...)

但在我声明数据网格中的所有列之前:

    DataGridTextColumn col1 = new DataGridTextColumn();
    DataGridTextColumn col2 = new DataGridTextColumn();
    DataGridTextColumn col3 = new DataGridTextColumn();
    dataGrid1.Columns.Add(col1);
    dataGrid1.Columns.Add(col2);
    dataGrid1.Columns.Add(col3);
    col1.Binding = new Binding("nazwa");
    col2.Binding = new Binding("cena");
    col3.Binding = new Binding("kod");
    col1.Header = "nazwa";
    col2.Header = "cena";
    col3.Header = "kod";

现在我已经添加了按输入文本进行过滤的功能,因此我在数据网格(每列)下有 3 个文本框,但它们的宽度与数据网格中列的宽度不同。我尝试过类似 textbox1.width = datagrid.columns[1].width 的想法,但这不起作用。 有人知道我的问题的解决方案吗?

感谢您的回答!

ps。我不能像这样声明宽度(例如 textbox.width = 200)

最佳答案

尝试类似的事情:

XAML 文件:

<Window x:Class="GridAutoWidth.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="30" />
        </Grid.RowDefinitions>
        <DataGrid Name="dgProducts" ItemsSource="{Binding Products}" />

        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="{Binding ElementName=dgProducts, Path=Columns[0].ActualWidth}" />
                <ColumnDefinition Width="{Binding ElementName=dgProducts, Path=Columns[1].ActualWidth}" />
                <ColumnDefinition Width="{Binding ElementName=dgProducts, Path=Columns[2].ActualWidth}" />
            </Grid.ColumnDefinitions>            
            <TextBox Grid.Column="0" />
            <TextBox Grid.Column="1" />
            <TextBox Grid.Column="2" />            
        </Grid>
    </Grid>
</Window>

代码隐藏文件:

using System.Windows;

namespace GridAutoWidth
{    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
        }
    }
}

主视图模型:

using System.Collections.ObjectModel;

namespace GridAutoWidth
{
    class MainViewModel
    {
        public MainViewModel()
        {
            for (int i = 0; i < 10; i++)
            {
                Products.Add(new Product 
                {
                    Name = "Name " + i,
                    Price = (decimal)(18 * (i + 8.4)),
                    Code = "Sample code " + i
                });
            }
        }

        private ObservableCollection<Product> _products = new ObservableCollection<Product>();

        public ObservableCollection<Product> Products
        {
            get { return _products; }
            set { _products = value; }
        }
    }

    public class Product
    {
        public string Name { get; set; }
        public decimal Price { get; set; }
        public string Code { get; set; }
    }
}

关于c# - 获取DataGrid宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12186123/

相关文章:

c# - 如何在 MVVM Caliburn.Micro 中绑定(bind)用户控件?

WPF:条件绑定(bind)与属性,XamlParseException 使用

c# - 在 lambda 表达式中使用 foreach 循环的迭代器变量 - 为什么会失败?

c# - YouTube C#API-如何恢复失败的上传

c# - 是否可以轮询任务是否完成?

c# - 将用户控件的属性绑定(bind)到数据

c# - WPF DataGrid 中的自定义复选框不更新绑定(bind)

C# 并发 - 长时间运行任务的首选方法

c# - 无法加载工具箱项目。它将从工具箱中删除

wpf - 如何在Wpf中设置图像的来源?