Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF中更改ListView中特定行的样式_C#_Wpf_Xaml_Listview - Fatal编程技术网

C# 如何在WPF中更改ListView中特定行的样式

C# 如何在WPF中更改ListView中特定行的样式,c#,wpf,xaml,listview,C#,Wpf,Xaml,Listview,我的窗口中有一个绑定到列表的ListView,现在我想根据该行的值更改特定行的样式。假设我希望其中具有特定值的行具有灰色背景。我该怎么做 <ListView Margin="0 10 0 0" Height="205" HorizontalAlignment="Center" VerticalAlignment="Top" Width="270" Name="Elev

我的窗口中有一个绑定到列表的ListView,现在我想根据该行的值更改特定行的样式。假设我希望其中具有特定值的行具有灰色背景。我该怎么做

<ListView Margin="0 10 0 0" 
            Height="205"
            HorizontalAlignment="Center"
            VerticalAlignment="Top" 
            Width="270"
            Name="ElevationList">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Story" 
            DisplayMemberBinding="{Binding Path=El}"
            Width="100"/>
        </GridView>
    </ListView.View>
</ListView> 

XAML代码:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:sampleApp="clr-namespace:WpfApplication1">
<Window.Resources>
    <sampleApp:NumberToBGColorConverter x:Key="NumberBGConverter"/>
</Window.Resources>
<Grid>
    <ListView ItemsSource="{Binding ListViewItems}">
        <ListView.ItemTemplate>
            <DataTemplate DataType="{x:Type sampleApp:Number}">
                <Grid Background="{Binding NumberValue,Converter={StaticResource NumberBGConverter}}" Height="20" Width="250">
                    <TextBlock Text="{Binding NumberValue}" FontWeight="SemiBold"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

C#代码:

公共部分类主窗口:窗口
{
私有列表m_ListViewItems;
公共列表ListViewItems
{
获取{return m_ListViewItems;}
设置{m_ListViewItems=value;}
}
公共主窗口()
{
初始化组件();
DataContext=this;
ListViewItems=新列表();
添加(新编号(){NumberValue=1});
添加(新编号(){NumberValue=2});
添加(新编号(){NumberValue=3});
添加(新编号(){NumberValue=4});
}
}
公共课号
{
私有整数m_数值;
公共整数值
{
获取{return m_NumberValue;}
设置{m_NumberValue=value;}
}
}
公共类编号bgColorConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
变量编号=(int)值;
如果(数字%2==0)
返回“灰色”;
其他的
返回“黄色”;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}

根据结果的数量,您可以使用自定义的
IValueConverter
public partial class MainWindow : Window
{
    private List<Number> m_ListViewItems;

    public List<Number> ListViewItems
    {
        get { return m_ListViewItems; }
        set { m_ListViewItems = value; }
    }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        ListViewItems = new List<Number>();
        ListViewItems.Add(new Number() { NumberValue = 1 });
        ListViewItems.Add(new Number() { NumberValue = 2 });
        ListViewItems.Add(new Number() { NumberValue = 3 });
        ListViewItems.Add(new Number() { NumberValue = 4 });
    }
}

public class Number
{
    private int m_NumberValue;

    public int NumberValue
    {
        get { return m_NumberValue; }
        set { m_NumberValue = value; }
    }
}

public class NumberToBGColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var number = (int)value;

        if (number % 2 == 0)
            return "Gray";
        else
            return "Yellow";
    }

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