如何根据状态值

时间:2016-08-02 14:18:39

标签: c# wpf

在datatemplate中有两个按钮,每行只能显示一个按钮,例如,如果Stuaus值为1,则显示button1,否则显示button2

注意:状态值来自数据库。

我正在使用WPF datagrid:

当前代码显示两者,我不知道如何根据状态值显示和隐藏按钮。 Updated Code

<DataGridTemplateColumn Header="Fungsi" Width="150" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
  <DataTemplate>
    <StackPanel Orientation="Horizontal">
     <Button Name="btn1" Content="Test1" Background="#FFCC00 "></Button>
     <Button Name="btn2" Content="Test2" Background="Blue"></Button>
    </StackPanel>
  </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

3 个答案:

答案 0 :(得分:1)

您可以使用converter参数创建转换器并进行处理。请参阅以下代码。同意@Sam,但如果他有更多状态,那么下面的代码将有所帮助。

<Window x:Class="DataGrid_Status.Window4"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DataGrid_Status"
    mc:Ignorable="d"
    Title="Window4" Height="300" Width="300">
<Grid>
    <Grid.Resources>
        <local:StatusConverter x:Key="StatusConverter"></local:StatusConverter>
    </Grid.Resources>
    <DataGrid x:Name="DataGrid" AutoGenerateColumns="False" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"></DataGridTextColumn>
            <DataGridTemplateColumn Header="Fungsi" Width="150" IsReadOnly="True" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Button Name="btn1" Content="Success" Background="Green"
                                    Visibility="{Binding Status,Converter={StaticResource StatusConverter},ConverterParameter=a}"></Button>
                            <Button Name="btn2" Content="InProg" Background="Yellow"
                                    Visibility="{Binding Status,Converter={StaticResource StatusConverter},ConverterParameter=b}"></Button>
                            <Button Name="btn3" Content="Failed" Background="Red"
                                    Visibility="{Binding Status,Converter={StaticResource StatusConverter},ConverterParameter=c}"></Button>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

    using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace DataGrid_Status
{
    /// <summary>
    /// Interaction logic for Window4.xaml
    /// </summary>
    public partial class Window4 : Window
    {
        public Window4()
        {
            InitializeComponent();
            List<MyClass> lst = new List<MyClass>();
            for (int i = 0; i < 10; i++)
            {
                MyClass obj = new MyClass();
                obj.Name = "Name" + i;
                if (i == 1)
                {
                    obj.Status=StatusEnum.Success;
                }
                else if (i == 2)
                {
                    obj.Status = StatusEnum.Failed;
                }
                else
                {
                    obj.Status = StatusEnum.InProgress;
                }
                lst.Add(obj);
            }
            DataGrid.ItemsSource = lst;
        }
    }

    class StatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            StatusEnum status = (StatusEnum)value;
            string param = parameter as string;
            if ((status == StatusEnum.Success && param == "a")|| (status == StatusEnum.InProgress && param == "b")|| (status == StatusEnum.Failed && param == "c"))
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    class MyClass
    {
        public string Name { get; set; }
        public StatusEnum Status { get; set; }
    }

    enum StatusEnum
    {
        Success,
        Failed,
        InProgress
    }
}

答案 1 :(得分:0)

Button Visible状态绑定到您在后面的代码中更新的bool,即:。

<Button Name="btn1" Content="Test1" Background="#FFCC00 " Visible="{Binding btn1Visible}"/>

并在后面的代码中:

public bool btn1Visible
{
     get { return _btn1Visible; }
     set
     {
         _btn1Visible = value;
         NotifyPropertyChanged("btn1Visible");
     }
}

其中NotifyPropertyChangedPropertyChangedEventHandler

部分def round_list(l): return [int(round(i, -1)) for i in l] 的包装器

答案 2 :(得分:0)

你必须以某种方式将按钮的可见性绑定到状态。

我通常使用bool到可见性转换器来执行此操作。 如果为false,则返回visible的可见性值,如果为false,则返回折叠。

您可以使用相同的功能,也可以根据状态变量创建自己的转换器。 例如,如果value == 1则返回true,否则返回false