如果我理解你的問題,這對我有用。我不知道你的布爾屬性被調用了什麼;我寫了一個叫IsOdd
。
這當任何行會打開一個彈出:
- 該行被選中,和
- 該行的
DataContext
有IsOdd
屬性等於False
的Popup
的DataContext
是該行的DataContext
。把任何你想要的XAML放在那裏,UserControl
或其他什麼。
這裏唯一真正讓我感到困擾的是行演示文稿XAML(SelectiveScrollingGrid
中的所有內容)。這種做法會破壞答案中的信噪比,但它必須在那裏。至少你已經掌握了它。
XAML:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRow}">
<Grid>
<!--
Stole content presentation from here:
http://stackoverflow.com/a/14266323/424129
-->
<SelectiveScrollingGrid>
<SelectiveScrollingGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</SelectiveScrollingGrid.ColumnDefinitions>
<SelectiveScrollingGrid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</SelectiveScrollingGrid.RowDefinitions>
<DataGridCellsPresenter
Grid.Column="1"
ItemsPanel="{TemplateBinding ItemsPanel}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
/>
<DataGridDetailsPresenter
Grid.Column="1"
Grid.Row="1"
SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
Visibility="{TemplateBinding DetailsVisibility}"
/>
<DataGridRowHeader
Grid.RowSpan="2"
SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"
Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
/>
</SelectiveScrollingGrid>
<Popup
x:Name="RowPopup"
IsOpen="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}}"
>
<Border
MinWidth="200"
MinHeight="166"
Background="WhiteSmoke"
BorderBrush="Black"
BorderThickness="1"
Padding="12"
>
<TextBlock>
<TextBlock Text="Blah blah popup. " />
<TextBlock Text="{Binding IsOdd, StringFormat=IsOdd: {0} }" />
</TextBlock>
</Border>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<!--
The RelativeSource=TemplatedParent binding doesn't seem to be working here.
I don't understand why not. Probably something stupid and obvious.
-->
<!--
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition
Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}}"
Value="True" />
<Condition Binding="{Binding IsOdd}" Value="False" />
</MultiDataTrigger.Conditions>
<Setter TargetName="RowPopup" Property="IsOpen" Value="True" />
</MultiDataTrigger>
-->
<!--
So instead, we bind IsOpen on the popup to IsSelected, and then
override that with False if the boolean property is true. Clumsy
but it works.
-->
<DataTrigger Binding="{Binding IsOdd}" Value="True">
<Setter TargetName="RowPopup" Property="IsOpen" Value="False" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
MVVM模式在這裏打破了...嘗試加入主持人使這個MVPVM模式。他們讓演示者在檢測到事件時執行彈出窗口。 – Aron