欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF自定义列中按钮的IsEnabled属性根据列中的值的不同动态赋值

程序员文章站 2022-03-01 13:01:56
...
1.创建转换类,0为已读,让“阅”按钮不可用,即返回False

public class ReadOrNoReadConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int readStatus = value.ToString();
if (readStatus==0)
{
return false;
}
else
{
return true;
}
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string strValue = value.ToString();
//DateTime resultDateTime;
//if (DateTime.TryParse(strValue, out resultDateTime))
//{
// return resultDateTime;
//}
return value;
}
}


2.WPF中在模板列写如下代码,IsEnabled的属性值是根据readStatus字段经过ReadOrNoReadConverter转换器的转换生成的,所以到底生成true还是false要在转换器类中定义逻辑规则

<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="阅" IsEnabled="{Binding Path=readStaus,Converter={StaticResource ReadOrNoReadConverter}}"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


3.最后引入命名空间

<UserControl x:Class="BankLogix.ForexStar.WinFXManager.Components.wpf.OpenPositions"
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:dataPrimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
xmlns:VM="clr-namespace:BankLogix.ForexStar.WinFXManager.Components.wpf.Converter"

声明转换器

<UserControl.Resources>
<VM:ReadOrNoReadConverter x:Key="ReadOrNoReadConverter"/>
</UserControl.Resources>
相关标签: WPF