我有一個簡單的OHLC物件,實作了INotifyChanged。每個OHLC物件都與一個特定的蠟燭有關,是List的一部分。每個List與一個特定的股票有關,并被存盤在一個ConcurrentDictionary<string, List>中。
我試圖將每只股票的最后一根蠟燭的資料系結到一個資料網格視圖中。這就是我如何設定資料庫系結的程序。這不是很優雅!
ConcurrentDictionary<string, List< OHLC>> candles= new ConcurrentDictionary<string, List<OHLC>> ()。
BindingList<OHLC> LastCandles = new BindingList<OHLC> ();
. . .
form1.dataGridView1.DataSource = LastCandles;
. . .
public void OnUpdate()
{
//更新相應的蠟燭。
. . .
//Pull out the last candle by symbol and re-bind
BindingList<OHLC> lastBySymbol = new BindingList<OHLC> ();
foreach (KeyValuePair<string, List<OHLC> > dict in candles)
{
if (dict.Value.Count > 0)
{
lastBySymbol.Add(dict.Value.Last())。
}
}
LastCandles = lastBySymbol;
form1.dataGridView1.BeginInvoke((MethodInvoker)delegate)
{
form1.dataGridView1.DataSource = LastCandles;
form1.dataGridView1.Refresh()。
});
}
在這里設定資料系結的最佳方式是什么,這樣我就可以專注于更新集合而不必在每次更新時重新系結?我可以節制對datagridview.Refresh()的呼叫,這樣它就不會在每次原子更新時重繪,但我不希望必須重新系結。
我知道如何對自定義物件的 BindingList 進行一次資料系結,并通過 INotifyPropertyChanged 和對 datagridview.Refresh() 的簡單呼叫將更新傳播到 UI。我也可以在XAML中找到一些例子,顯示了對BindingList中最后一個專案的資料系結。但是我找不到任何例子可以系結到串列字典中的 "最后一個鍵"。
如果能在這方面得到任何幫助,我將不勝感激。
uj5u.com熱心網友回復:
注意:這將是一個WPF問題的解決方案,而不是WinForms.
。不要轉換你的資料來適應視圖,創建一個值轉換器來處理這種轉換。
[ValueConversion(typeof(System.Collections.IEnumerable), typeof(object))/span>]
public class LastItemConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
((System.Collections.IEnumerable)value)? .Cast<object>().Last();
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
throw new NotSupportedException();
然后使用資料系結應用轉換:
YourControl.xaml.cs:
public partial class YourControl : UserControlpublic ConcurrentDictionary<string, List<OHLC>> Candles { get; } = ...。
}
YourControl.xaml:
<UserControl x:Class="YourNamespace.YourControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"/span>
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"。
xmlns:local="clr-namespace:YourNamespace"
DataContext="{Binding RelativeSource={RelativeSource Self}}"/span>>
<UserControl.Resources>/span>
<local:LastItemConverter x:Key="lastItemConverter" />/span>
</UserControl.Resources>
<Grid>/span>
< DataGrid ItemsSource="{binding Candles}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Key"/span>
Binding="{Binding Key}"/span> />
<DataGridTextColumn Header="Value"
Binding="{Binding Value, Converter={StaticResource lastItemConverter}}" />
</DataGrid.Columns>
</DataGrid>/span>
</Grid>/span>
</UserControl>
根據需要觸發更新。
uj5u.com熱心網友回復:
Windows Forms的DataGridView有一種方法可以通過CellFormatting事件覆寫它的資料格式。處理該事件,所以你可以改變將用于該列的值。如果你想的話,你也可以在這里對單元格進行樣式設計。
form1.dataGridView1.CellFormatting = this.dataGridView1_CellFormatting。
//....
private void dataGridView1_CellFormatting(objectsender, DataGridViewCellFormattingEventArgs e)。
{
switch (dataGridView1.Columns[e.ColumnIndex] .Name)
{
case "Value"/span>: // 假設這是對應的列名。
if (e.Value is List<OHLC> asList)
{
var lastValue = asList.Last()。
e.Value = lastValue。
//span>額外造型的例子。
if (lastValue.CostSavings > 0)
e.CellStyle.ForeColor = Color.Green。
}
return;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/309685.html
標籤:
