2016-07-18 62 views
0

我有一個WPF Datagrid列2包含某些值。我想這樣做,如果用戶單擊第1行中的任何單元格,並且(第1行,第2列)中的值爲true,則會彈出用戶控件。Datagrid觸發器基於值的用戶控件

我知道如何使用

    <DataGrid.RowStyle> 
        <Style TargetType="DataGridRow"> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding itemCtrlType}" Value="true"> 
           <Setter Property="Background" Value="Aqua"/> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </DataGrid.RowStyle> 

我在想試圖窩在這個事件觸發以改變該行的顏色,但我難倒就如何做到這一點。我是否以這種錯誤的方式去做?

+0

MVVM模式在這裏打破了...嘗試加入主持人使這個MVPVM模式。他們讓演示者在檢測到事件時執行彈出窗口。 – Aron

回答

1

如果我理解你的問題,這對我有用。我不知道你的布爾屬性被調用了什麼;我寫了一個叫IsOdd

這當任何行會打開一個彈出:

  • 該行被選中,
  • 該行的DataContextIsOdd屬性等於False

PopupDataContext是該行的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>