Silverlight DataGridTextColumn Binding Visibility
我需要一个Silverlight DataGrID中的列可以基于viewmodel中的值来显示/折叠.要完成这个,我试图将Visibility属性绑定到viewmodel.但是我很快发现Visibility属性不是DependencyProperty,因此它不能绑定.
为了解决这个问题,我试图将自己的DataGrIDTextColumn子类化.有了这个新类,我创建了一个DependencyProperty,它最终将更改推送到DataGrIDTextColumn.Visibility属性.这样做很好,如果我不数据绑定.当我将数据绑定到我的新属性时,它会失败,并出现AG_E_PARSER_BAD_PROPERTY_VALUE异常.
public class MyDataGrIDTextColumn : DataGrIDTextColumn{ #region public Visibility MyVisibility public static Readonly DependencyProperty MyVisibilityProperty = DependencyProperty.Register("MyVisibility",typeof(Visibility),typeof(MyDataGrIDTextColumn),new PropertyMetadata(Visibility.Visible,OnMyVisibilityPropertyChanged)); private static voID OnMyVisibilityPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) { var @this = d as MyDataGrIDTextColumn; if (@this != null) { @this.OnMyVisibilityChanged((Visibility)e.oldValue,(Visibility)e.NewValue); } } private voID OnMyVisibilityChanged(Visibility oldValue,Visibility newValue) { Visibility = newValue; } public Visibility MyVisibility { get { return (Visibility)GetValue(MyVisibilityProperty); } set { SetValue(MyVisibilityProperty,value); } } #endregion public Visibility MyVisibility}
这是XAML的一小部分.
<DataGrID ....> <DataGrID.Columns> <MyDataGrIDTextColumn header="User name" Foreground="#FFFFFFFF" Binding="{Binding User.Username}" MinWIDth="150" CanUserSort="True" CanUserResize="False" CanUserReorder="True" MyVisibility="{Binding Converter={StaticResource BoolToVisibilityConverter},Path=ShouldShowUser}"/> <DataGrIDTextColumn .../> </DataGrID.Columns></DataGrID>
几个重要的事实.
>转换器的确定在上面的本地资源中.
>转换器是正确的,它在解决方案中使用了许多其他地方.
>如果我用“折叠”替换MyVisibility属性的{Binding}语法,那么列实际上消失了.
>如果我创建一个新的DependencyProperty(即字符串Foo),并绑定到它,我也收到AG_E_PARSER_BAD_PROPERTY_VALUE异常.
有人有什么想法为什么这不工作?
解决方法 这是解决方案,我想到了一个小黑客.首先,您需要继承DataGrID.
public class DataGrIDEx : DataGrID{ public IEnumerable<string> HIDdenColumns { get { return (IEnumerable<string>)GetValue(HIDdenColumnsproperty); } set { SetValue(HIDdenColumnsproperty,value); } } public static Readonly DependencyProperty HIDdenColumnsproperty = DependencyProperty.Register ("HIDdenColumns",typeof (IEnumerable<string>),typeof (DataGrIDEx),new PropertyMetadata (HIDdenColumnsChanged)); private static voID HIDdenColumnsChanged(object sender,DependencyPropertyChangedEventArgs args) { var dg = sender as DataGrID; if (dg==null || args.NewValue == args.oldValue) return; var hIDdenColumns = (IEnumerable<string>)args.NewValue; foreach (var column in dg.Columns) { if (hIDdenColumns.Contains ((string)column.GetValue (nameProperty))) column.Visibility = Visibility.Collapsed; else column.Visibility = Visibility.Visible; } }}
DataGrIDEx类根据DataGrIDColumn及其后代的x:name名称添加新的DP来隐藏列.
在您的XAML中使用:
<my:DataGrIDEx x:name="uIData" DataContext="{Binding SomeDataContextFromTheVM}" ItemsSource="{Binding Whatever}" HIDdenColumns="{Binding HIDdenColumns}"> <sdk:DataGrIDTextColumn x:name="uIDataCountOfItems"> header="Count" Binding={Binding CountOfItems}" </sdk:DataGrIDTextColumn></my:DataGrIDEx>
您需要将这些添加到您的viewmodel或您使用的任何数据上下文.
private IEnumerable<string> _hIDdenColumns;public IEnumerable<string> HIDdenColumns{ get { return _hIDdenColumns; } private set { if (value == _hIDdenColumns) return; _hIDdenColumns = value; PropertyChanged (this,new PropertyChangedEventArgs("HIDdenColumns")); }}public voID SomeWhereInYourCode (){ HIDdenColumns = new List<string> {"uIDataCountOfItems"};}
要取消隐藏,您只需要从列表中删除相应的名称或重新创建它,而不使用未隐藏的名称.
总结以上是内存溢出为你收集整理的silverlight – 如何绑定DataGridColumn.Visibility?全部内容,希望文章能够帮你解决silverlight – 如何绑定DataGridColumn.Visibility?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)