`

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

    博客分类:
  • WPF
WPF 
阅读更多
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>
1
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics