DataGrid列宽度不自动更新(DataGrid column width doesn't auto-update)

   IT问题网   2019-08-13 00:00:00

问 题

  datagridtextcolumn绑定="{结合名}"width ="*"/
 datagridtextcolumn绑定="{结合而改变}"width ="自动"/
 

当值"变革"的更新,这列不更新,以适应新的价值。所以列停留过小和值进行剪裁。
任何想法?

解决方案

数据网格将增加列的大小,以适应作为数据变长,但它不会自动降低列大小时的数据的长度减少。在你的榜样,你是对的对齐"更改"专栏,并使用空间的其余部分的"名称"栏中。

现在,当一个"改变"房地产增长足够大,它应该增加列的宽度,在"名称"列拒绝缩小以适应,所以你必须强制刷新自己。

下面的步骤应该为你做这个(我已经包括了一个示例应用程序来演示):

1)在你的datagridtextcolumn绑定(除了你*大小的列)设置notifytargetupdated = true。
2)在您的datagrid,一个处理程序添加到targetupdated事件。
3)在你的targetupdated事件处理程序:
-a)设置datagrid的*大小的列的宽度设置为0。
-b)要求在datagrid中updatelayout请()方法
。 -c)设置datagrid的*大小的列的宽度回到新datagridlength(1,datagridlengthunittype.star)

示例xaml:

 窗口x:类="datagridtest.mainwindow"
的xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation"
的xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
标题="主窗口"高度="350"宽度="525"
 window.resources
 collectionviewsource x:关键="myobjectcollection"/
 /window.resourcesgt;
 dockpanel中
按钮dockpanel.dock ="底部"content ="点击,使项目1s文本不再是"点击="button_click"/
电网
数据网格x:名称="dg"的itemssource ="{绑定源= {的staticresource myobjectcollection}}"的autogeneratecolumns ="false"targetupdated ="dg_targetupdated"
 datagrid.columns
 datagridtextcolumn绑定="{结合第一}"width ="1 *"/
 datagridtextcolumn绑定="{结合去年,notifyontargetupdated = true}"width ="自动"/
 /datagrid.columnsgt;
 /数据网格
 /网格

 / dockpanel中
 /窗
 

为例code背后:

 使用系统;
使用system.collections.objectmodel;
使用system.windows;
使用system.windows.controls的;
使用system.windows.data;
使用system.componentmodel;

命名空间datagridtest
{
///总结
///为mainwindow.xaml交互逻辑
/// /总结
公共部分类主窗口:窗口
{
私人的observablecollection为myobject myobjectlist =新的observablecollection为myobject();

公共主窗口()
{
的initializecomponent();
(this.findresource("myobjectcollection")作为collectionviewsource).source = this.myobjectlist;
this.myobjectlist.add(新的myobject(){首先="鲍勃",最后="琼斯"});
this.myobjectlist.add(新的myobject(){首先="简",最后="李四"});
}

私人无效button_click(对象发件人,routedeventargs e)
{
this.myobjectlist [0]。去年="billyobrian";
}

私人无效dg_targetupdated(对象发件人,datatransfereventargs e)
{
dg.columns [0] .width = 0;
dg.updatelayout();
dg.columns [0] .width =新datagridlength(1,datagridlengthunittype.star);
}
}

公共类为myobject:inotifypropertychanged的
{
私人字符串的firstname;
公共字符串第一
{
{返回this.firstname; }
组
{
如果(this.firstname!=值)
{
this.firstname =价值;
notifypropertychanged("第一");
}
}
}

私人字符串的lastname;
公共字符串最后
{
{返回this.lastname; }
组
{
如果(this.lastname!=值)
{
this.lastname =价值;
notifypropertychanged("最后");
}
}
}

公众为myobject(){}

#地区 -inotifypropertychanged的合同 - 

公共事件propertychangedeventhandler的propertychanged;

私人无效notifypropertychanged(字符串信息)
{
如果(的propertychanged!= null)
{
的propertychanged(这一点,新propertychangedeventargs(信息));
}
}

#endregion inotifypropertychanged的合同
}
}
 

标签:宽度自动更新



分享:



热门推荐

即重新present树对象(Objects that represent trees)

problem are there any objects in c# (or in .net) that rep ...

.NET:是否有可能得到的HttpWebRequest自动DECOM preSS gzip压缩的反应?(.NET: Is it possible to get HttpWebRequest to automatically decompress gzip'd responses)

problem in this answer, i described how i resorted to wra ...

如何检查是否一个程序正在使用.NET?(How to check if a program is using .NET)

problem can we check if a running application or a progra ...

使用Visual Studio安装项目以自动注册和GAC一个COM互操作DLL(Use Visual Studio Setup Project to automatically register and GAC a COM Interop DLL)

problem i've created a .net assembly for com interop and ...

如何获得MX记录与System.Net.DNS DNS名称?(How to get mx records for a dns name with System.Net.DNS)

problem is there any built in method in the .net library ...

我为什么要preFER单曲“等待Task.WhenAll”在多个等待着?(Why should I prefer single 'await Task.WhenAll' over multiple awaits)

problem in case i don't care about the order ...

我该如何分析和转换日期时间的到RFC 822的日期,时间格式?(How do I parse and convert DateTime’s to the RFC 822 date-time format)

problem how do i convert a datetime structure to its equi ...

有什么区别呢.AsNoTracking()做什么呢?(What difference does .AsNoTracking() make)

problem i have a question regarding the .asnotracking() e ...

实体框架/ LINQ到SQL:跳过和放大器;采取(Entity Framework/Linq to SQL: Skip & Take)

problem just curious as to how skip amp; take are suppose ...

StreamWriter.Flush()和StreamWriter.Close()之间的区别是什么?(What is the difference between StreamWriter.Flush() and StreamWriter.Close())

problem what is the difference in functionality between s ...

现净switch语句散列或索引?(Are .Net switch statements hashed or indexed)

problem does .net 4 (or any prior version) perform any so ...

使用LINQ生成没有选择直接更新(Use linq to generate direct update without select)

problem g'day everyone. i'm still learning l ...

是否有可能拦截控制台输出?(Is it possible to intercept Console output)

problem i call a method, say, fizzbuzz(), over which i ha ...

当使用排序列表 TKEY的,TValue在一个SortedDictionary TKEY的,TValue?(When to use a SortedList<TKey, TValue> over a SortedDictionary<TKey, TValue>)

problem this may appear to be a duplicate of this questio ...

如何初始化列表 T给定的尺寸(相对于容量)?(How to initialize a List<T> to a given size (as opposed to capacity))

problem .net offers a generic list container whose perfor ...

如何使用XPath用的XDocument?(how to use XPath with XDocument)

problem there is a similar question, but it seems that th ...

什么是WebConfigurationManager和ConfigurationManager中的区别?(What's the difference between the WebConfigurationManager and the ConfigurationManager)

problem what's the difference between the webconfiguratio ...

NUnit的测试运行秩序(NUnit Test Run Order)

problem by default nunit tests run alphabetic ...

如何恢复从C#数据库(How to restore a database from C#)

problem i have a sql 2008 db. i am running a form that ba ...

调用堆栈不说"你来自哪里"但是"你要去哪里,下一个"?(The call stack does not say "where you came from", but "where you are going next")

problem in a previous question (get object ca ...