![](/img/trans.png)
[英]How to bind IsSelected property of DataGrid row to a source property
[英]WPF DataGrid row's color multibound (also) to own IsSelected property?
我有一個 DataGrid,其 ItemsSource 連接到 SQL 表。 我需要對行背景進行顏色編碼。 所以我為 DataRow 設置了一個樣式:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource rowColor}">
// here I'm passing DataRowView that gives me access to row data - this is working well
<Binding />
// below part is not working (compiling, but passing nothing)
<Binding Source="{RelativeSource Mode=Self}" Path="IsSelected" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
事情是我需要根據行的數據設置許多 colors。 即綠色、黃色、紅色......這是為了立即視覺識別每個項目的狀態。 這部分工作完美。
但我還需要對每一行進行顏色編碼,當它位於選定的 state 中時。 否則,一旦選擇,它們都將具有無用的默認 RoyalBlue 背景。 所以對於選定的行,我需要設置 DarkGreen、DarkYellow、DarkRed... 等。因此,MultiBinding 和我嘗試將自己的 IsSelected 屬性傳遞給轉換器。 那是行不通的。 無論我嘗試了什么(我嘗試了很多,還有其他屬性),在我的 Convert() function 中,values[0] 都可以(DataRowView),但 values[1] 始終是 DependencyObject.Unset。
這樣做的正確方法是什么?
嘗試設置RelativeSource
屬性而不是Source
:
<Binding RelativeSource="{RelativeSource Self}" Path="IsSelected" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.