Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.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# 根据条件更改ListView前景色_C#_.net_Wpf_Xaml_Data Binding - Fatal编程技术网

C# 根据条件更改ListView前景色

C# 根据条件更改ListView前景色,c#,.net,wpf,xaml,data-binding,C#,.net,Wpf,Xaml,Data Binding,我是WPF的新手,所以目前对这个概念相当困惑。 我有一个股票程序,我想根据股票价格是上涨还是下跌来改变单元格的前颜色 这是我当前的代码(省略了一些内容): 公共类股票:INotifyPropertyChanged { 公开股() { DaysLow=0; DaysHigh=0; } public List StockInformation=新列表(); 公共字符串符号{get;set;} 私人双标; 公开双标 { 获取{return\u Bid;} 设置 { //如果_Bid

我是WPF的新手,所以目前对这个概念相当困惑。 我有一个股票程序,我想根据股票价格是上涨还是下跌来改变单元格的前颜色

这是我当前的代码(省略了一些内容):

公共类股票:INotifyPropertyChanged
{
公开股()
{
DaysLow=0;
DaysHigh=0;
}
public List StockInformation=新列表();
公共字符串符号{get;set;}
私人双标;
公开双标
{
获取{return\u Bid;}
设置
{
//如果_Bid<值,则更改前颜色
_出价=价值;
DisplayCurrentPrice=String.Format(“{0}/{1}”,值,Ask);
}
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串PropertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(PropertyName));
}
}
公共重写字符串ToString()
{
返回符号;
}
}
XAML:

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Height" Value="26" />
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView AllowsColumnReorder="False">
                <GridViewColumn Header="Symbol" DisplayMemberBinding="{Binding Path=Symbol}" Width="120" />
                <GridViewColumn Header="Bid / Ask" DisplayMemberBinding="{Binding Path=DisplayCurrentPrice}" Width="125" />
                <GridViewColumn Header="D.High" DisplayMemberBinding="{Binding Path=DaysHigh}" Width="75" />
                <GridViewColumn Header="D.Low" DisplayMemberBinding="{Binding Path=DaysLow}" Width="75" />
                <GridViewColumn Header="Year Low/High" DisplayMemberBinding="{Binding Path=DisplayYearPrice}" Width="100" />
            </GridView>
        </ListView.View>
        <ListView.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Details" Click="CM_Details_Click"></MenuItem>
            </ContextMenu>
        </ListView.ContextMenu>
    </ListView>


那么,我在这里能做些什么来实现我所追求的目标呢?感谢您的帮助,谢谢

您可以将此添加到您的c中#

public bool ChangeColor{get;set;}
私人双标;
公开双标
{
获取{return\u Bid;}
设置
{
如果(_Bid
然后在xaml中

       <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Height" Value="26" />
                <Style.Triggers>
                   <DataTrigger Binding="{Binding Path=ChangeColor}" Value="True">
                      <Setter Property="ForeColor" Value="Red"/>
                   </DataTrigger >
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>

以下是我将如何表示我的
股票模型
类:

class StockModel : INotifyPropertyChanged
{
    // NOTE: You must implement PropertyChanged notification for these properties...
    public string Symbol { get; set; }
    public decimal Bid { get; set; }
    public decimal Delta { get; set; }  // Change in price over time, +/-

    // Any additional properties you may want here...
}
现在,WPF的美妙之处在于类的形式。这些类允许您使用提供的逻辑将值从一种类型“转换”为另一种类型

在本例中,我们希望将
ListViewItem.Foreground
颜色绑定到
StockModel.Delta
值。正的非零增量值应为我们提供
绿色
,负的非零增量值应为我们提供
红色
。零可以保持为
黑色
白色
(取决于主题的对比度)

那么,我们如何完成这样一件事呢?我们需要创建一个实现
IValueConverter
StockColorConverter
类:

sealed class StockColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // Convert a delta value to a brush color.
        var deltaValue = System.Convert.ToDecimal(value);

        if (deltaValue > 0)
            return Brushes.Green;       // Positive
        else if (deltaValue < 0)
            return Brushes.Red;         // Negative 
        else
            return Brushes.Black;       // Zero
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // We can't convert a color to a delta value! This will never be used anyhow.
        throw new NotSupportedException();
    }
}
然后,创建(或附加到)
窗口。参考资料

<Window.Resources>
    <my:StockColorConverter x:Key="StockColorConverter"/>
</Window.Resources>

您可以看到,我们正在将
TextBlock.Foreground
绑定到
StockModel.Delta
。通常这不起作用,因为
decimal
不等同于
Brush
对象。但是,使用我们创建的
StockColorConverter
,我们可以轻松地应用条件格式。

更改哪个单元格的
ForeColor
,条件是什么?您好,Rohit,我想更改单元格显示CurrentPrice,条件是Bidvalue=red您需要向数据模型中添加一些内容来表示行应该是什么颜色,并使用
DataTrigger
,或者使用
IValueConverter
将数据转换为
前台
属性的颜色。您可能需要一个
IMultiValueConverter
,这样您就可以将
Bid
Value
参数传递给它,并让它返回您的颜色。这与
IValueConverter
相同,只是它接受多个参数。:)是的,我知道现在需要做什么。假设我有一把公共刷子。如何将其绑定到GridViewColumn?我是XAML最差的。
sealed class StockColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // Convert a delta value to a brush color.
        var deltaValue = System.Convert.ToDecimal(value);

        if (deltaValue > 0)
            return Brushes.Green;       // Positive
        else if (deltaValue < 0)
            return Brushes.Red;         // Negative 
        else
            return Brushes.Black;       // Zero
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // We can't convert a color to a delta value! This will never be used anyhow.
        throw new NotSupportedException();
    }
}
xmlns:my="clr-namespace:WpfApplication1"
<Window.Resources>
    <my:StockColorConverter x:Key="StockColorConverter"/>
</Window.Resources>
        <ListView.ItemTemplate>
            <!-- These are StockModel objects as our data! -->
            <DataTemplate>
                <TextBlock Text="{Binding Symbol}"
                           Foreground="{Binding Delta, Converter={StaticResource StockColorConverter}}"/>
            </DataTemplate>
        </ListView.ItemTemplate>