[英]c# wpf DataGrid: programmatically changing image source on bound columns
我有一個SQL數據庫,其中計算機名稱綁定到DataGrid,如下所示:
<Grid.Resources>
<ResourceDictionary>
<local:FileIconConverter2 x:Key="FileIconConverter2"/>
<BitmapImage x:Key="Image-Unknown" UriSource="../Images/Status-Unknown.ico" />
<BitmapImage x:Key="Image-Good" UriSource="../Images/Status-Good.ico" />
</ResourceDictionary>
</Grid.Resources>
<DataGrid Name="datagrid1" AutoGenerateColumns="False" IsReadOnly="True" SelectionChanged="datagrid1_SelectionChanged">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Status" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{StaticResource Image-Unknown}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Date d m yyyy" Binding="{Binding 'Date d m yyyy'}"/>
<DataGridTextColumn Header="Device Name" Binding="{Binding DeviceName}"/>
<DataGridTextColumn Header="OS" Binding="{Binding OS}"/>
<DataGridTextColumn Header="OS" Binding="{Binding Model}"/>
<DataGridTextColumn Header="EOL" Binding="{Binding DeviceAge}"/>
<DataGridTextColumn Header="Location" Binding="{Binding Location}"/>
<DataGridTextColumn Header="IP" Binding="{Binding IP}"/>
<DataGridTextColumn Header="MAC" Binding="{Binding Mac}"/>
</DataGrid.Columns>
我想做的是當您單擊一行時,通過更改圖像來更新狀態列圖像以顯示計算機是否在線。 “狀態”列不在數據庫中。 每當我嘗試更改狀態列的內容時,都會得到[icon]或[bitmap]文本,而不是圖像,如果我向下滾動,則備份時圖像默認返回。
這是任何建議嗎?
編輯:表可以包含100個條目的地方,我只想在選中一行時檢查在線狀態。 所以我試圖像這樣在datagrid1上使用SelectionChanged事件:
private void datagrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex);
DataGridCell rowColumn = dataGrid.Columns[2].GetCellContent(row).Parent as DataGridCell;
string CellValue = ((TextBlock)rowColumn.Content).Text;
log.Warn(CellValue);
//myclass.GetCell(datagrid1, row.GetIndex(), 0).Content = Properties.Resources.Status_Good;
myclass.GetCell(datagrid1, row.GetIndex(), 0).Content = (Bitmap)System.Drawing.Image.FromFile(@".\Images\Status-good.ico");
}
myClass.GetCell代碼來自這篇文章。
但是,正如您在此屏幕快照中看到的那樣,我只得到文本,而不是圖像。 屏幕截圖
您是否嘗試向圖像添加觸發器?
<DataGridTemplateColumn Header="Status" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image >
<Image.Style>
<Setter Property="Source" Value="{StaticResource Image-Unknown}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding YourModelPropertyStatus}" Value="true">
<Setter Property="Source" Value="{StaticResource Image-Good}"/>
</DataTrigger>
</Style.Triggers>
</Image.Style>
</Image>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
編輯:
解決方案1(最接近您的現有代碼)
與其在后面的代碼中搜索和編輯單元格內容(這確實是一種錯誤做法), e.AddedItems
和e.RemovedItems
的selectionChanged回調函數所提供的參數,這些參數將為您提供已經存在的業務模型(代表設備)。添加和刪除。 就像是
foreach (var addedItem in e.AddedItems)
{
var device = (Device) addedItem;
device.CheckStatus();
}
foreach (var remItem in e.RemovedItems)
{
var device = (Device)remItem;
device.SetToUnknown();
}
其中CheckStatus
是檢查在線狀態的方法,它將標志YourModelPropertyStatus
設置為true或false(從而激活上述觸發器)
解決方案2(最干凈的方法)
使觸發器解決方案保持在XAML中的最佳,最簡單的方法是使用CollectionView
封裝設備集合來管理視圖模型中的所有內容。 在XAML DataGrid中,設置IsSynchronizedToCurrentItem=true
,然后在視圖模型中,訂閱CurrentChanged
事件,然后在回調中獲取CollectionView
的CurrentItem並調用CheckStatus();
可以在您的視圖模型中,也可以在您的設備對象中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.