我有一個DataGridComboBoxColumn
列一個DataGrid,我需要啓用或禁用另一列複選框細胞,但我的代碼似乎並沒有工作。禁用在WPF數據網格中的單元格時,組合值設置爲一個值 - MVVM
我有下面的代碼定義我的組合列:
<DataGridComboBoxColumn Header="Location"
SelectedValueBinding="{Binding LocationId, Mode=TwoWay}"
SelectedValuePath="LocationId"
DisplayMemberPath="LocationText"
TextBinding="{Binding Location, Mode=TwoWay}">
,這是我的複選框列代碼:我添加了一個DataTrigger嘗試檢測時
<DataGridCheckBoxColumn Header="Active"
Binding="{Binding IsActive}">
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedLineItem.Location}"
Value="01">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
位置字段更改爲01,並基於此我試圖禁用所選行中的複選框單元格,但它不起作用。
我還需要檢查/取消選中基於其他值的組合。
任何想法,我可以如何實現這一目標?
更新1:
好吧,我已經取得了一些進展,其中我現在的觸發器踢,但出於某種原因,每當我在降改變位置下移選定(僥倖!)單元格,它將所有行的值更改爲相同的值。
這裏是我的下拉列全碼:
<DataGridComboBoxColumn Header="Location"
SelectedItemBinding="{Binding DataContext.SelectedLineItem.SelectedLocation,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},
Mode=TwoWay}"
SelectedValuePath="LocationId"
DisplayMemberPath="LocationText"
TextBinding="{Binding Location, Mode=TwoWay}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding DataContext.Locations,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="FontSize" Value="14"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding DataContext.Locations,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}}"/>
<Setter Property="SelectedItem" Value="{Binding DataContext.SelectedLocation,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}}"/>
<Setter Property="IsReadOnly" Value="False"/>
<Setter Property="IsEditable" Value="True"/>
<Setter Property="FontSize" Value="14"/>
<EventSetter Event="KeyUp" Handler="LocationColumn_KeyUp"></EventSetter>
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsDropDownOpen" Value="true" />
</Trigger>
</Style.Triggers>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
這裏是我的複選框列的完整代碼(不要擔心什麼觸發器做的 - 他們是測試觸發器):
<DataGridCheckBoxColumn Header="Active" Binding="{Binding IsActive}">
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedLocation.LocationId}" Value="1">
<Setter Property="Background" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding SelectedLocation.LocationText}" Value="" >
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding SelectedLocation.LocationText}" Value="01" >
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
<DataTrigger Binding="{Binding SelectedLocation.LocationText}" Value="02" >
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding SelectedLocation.LocationText}" Value="03" >
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
誰能弄清楚爲什麼當我改變一個組合的值我的所有行現在正在設定?
謝謝。
進行裝訂可能不是你認爲它是DataContext的;它是否適用於'RelativeSource AncestorType = DataGrid'? –
@EdPlunkett我已經更新了我的答案。進一步,但仍然不是100%。有任何想法嗎? – Thierry