【发布时间】:2013-12-26 17:52:00
【问题描述】:
我是 WPF 的新手,所以目前对这个概念很困惑。 我有一个股票程序,我想根据股票价格是上涨还是下跌来更改该单元格的前景色。
这是我当前的代码(省略了几件事):
public class Stock : INotifyPropertyChanged
{
public Stock()
{
DaysLow = 0;
DaysHigh = 0;
}
public List<string[]> StockInformation = new List<string[]>();
public string Symbol { get; set; }
private double _Bid;
public double Bid
{
get { return _Bid; }
set
{
// If _Bid < value, change fore color
_Bid = value;
DisplayCurrentPrice = String.Format("{0} / {1}", value, Ask);
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
public override string ToString()
{
return Symbol;
}
}
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>
那么我可以在这里做些什么来实现我正在寻找的东西?任何帮助表示赞赏,谢谢!
【问题讨论】:
-
更改
ForeColor哪个单元格以及在什么条件下? -
嗨 Rohit,我想更改单元格 DisplayCurrentPrice,条件是 Bid value = Red
-
您需要在数据模型中添加一些内容来表示行应该是什么颜色并使用
DataTrigger,或者使用IValueConverter将您的数据转换为Foreground的颜色财产。您可能需要一个IMultiValueConverter,这样您就可以将Bid和Value参数传递给它,并让它返回您的颜色。这与IValueConverter相同,但它接受多个参数。 :) -
是的,我明白现在需要做什么。假设我有一个公共画笔_Bruch {get;set;}。我如何将它绑定到 GridViewColumn?我是 XAML 最差的。
标签: c# .net wpf xaml data-binding