在 WPF 中,我有一個 6 列的資料網格,我用資料填充我的資料網格,我想做的是在我的第一列“Tag_Name”中的第一個值旁邊放一個小圓圈,每個圓圈都有不同的顏色排。現在我被卡住了我創建了一個堆疊面板然后我創建了 myEllipse 但我不知道如何在 Tag_Name 旁邊附加 Ellipse

public class Table
{
public string Tag_Name { get; set; }
public string Tag_Comment { get; set; }
public float Value { get; set; }
public string Unit { get; set; }
public float Lower { get; set; }
public float Upper { get; set; }
}
public List<Table> datagrid_List = new List<Table>();
StackPanel myStackPanel = new StackPanel();
// Create a red Ellipse.
Ellipse myEllipse = new Ellipse();
// Create a SolidColorBrush with a red color to fill the
// Ellipse with.
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
// Describes the brush's color using RGB values.
// Each value has a range of 0-255.
mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
myEllipse.Fill = mySolidColorBrush;
myEllipse.StrokeThickness = 2;
myEllipse.Stroke = Brushes.Black;
// Set the width and height of the Ellipse.
myEllipse.Width = 8;
myEllipse.Height = 2;
myStackPanel.Children.Add(myEllipse);
在這里,我將資料附加到我的網格視圖中,但我不知道如何在標簽名稱旁邊添加堆疊面板
datagrid_List.Add(new Table { Tag_Name = myStackPanel "Tag1" , Tag_Comment = "", Value = 8, Unit = "kj/h", Lower = 9, Upper = 28 });
datagrid_List.Add(new Table { Tag_Name = myStackPanel "Tag2", Tag_Comment = "", Value = 45, Unit = "kj/h", Lower = 9, Upper = 12 });
datagrid_List.Add(new Table { Tag_Name = myStackPanel "Tag3", Tag_Comment = "", Value = 25, Unit = "kj/h", Lower = 9, Upper = 98 });
dataGrid.ItemsSource = datagrid_List;
uj5u.com熱心網友回復:
在 XAML 標記中DataGridTemplateColumn使用 a定義 a并將其放入:CellTemplateStackPanel
<DataGrid x:Name="datagrid_List" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Tag Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Ellipse Width="8" Height="2" Fill="Red" />
<TextBlock Text="{Binding Tag_Name}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Tag Comment" Binding="{Binding Tag_Comment}" />
...
</DataGrid.Columns>
</DataGrid>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/527047.html
標籤:C#wpf
