2017-02-06 57 views
1

工作,我有一个ComboBox,通过改变DataTemplate包括CheckBox,对自己做的时候,但移动时它变成一个DataGridCheckBox无法点击此工作正常。WPF复选框不会在DataGrid中

工作代码:

<ComboBox ItemsSource="{Binding WrapUpHelper.WrapUps}"> 
       <ComboBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Vertical"> 
          <StackPanel Orientation="Horizontal"> 
           <CheckBox IsChecked="{Binding IsSelected}" 
              Width="20" /> 
           <TextBlock Text="{Binding FriendlyDescription}" /> 
          </StackPanel> 
          <ListBox ItemsSource="{Binding WrapUps}" 
            Visibility="{Binding Path=IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}" 
            BorderThickness="0" 
            Background="Transparent"> 
           <ListBox.ItemTemplate> 
            <DataTemplate> 
             <StackPanel Orientation="Horizontal"> 
              <CheckBox IsChecked="{Binding IsSelected}" 
                 Width="20" /> 
              <TextBlock Text="{Binding FriendlyDescription}" /> 
             </StackPanel> 
            </DataTemplate> 
           </ListBox.ItemTemplate> 
          </ListBox> 

         </StackPanel> 
        </DataTemplate> 
       </ComboBox.ItemTemplate> 
      </ComboBox> 

证明:

enter image description here

但添加此相同的逻辑来一个DataGrid的时候,而不是添加勾选的CheckBoxComboBox什么也没有关闭选择。

非工作代码:

<DataGridTemplateColumn Header="Wrap up" Width="100"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <ComboBox ItemsSource="{Binding WrapUpHelper.WrapUps}"> 
           <ComboBox.ItemTemplate> 
            <DataTemplate> 
             <StackPanel Orientation="Vertical"> 
              <StackPanel Orientation="Horizontal"> 
               <CheckBox IsChecked="{Binding IsSelected}" 
                  Width="20" /> 
               <TextBlock Text="{Binding FriendlyDescription}" /> 
              </StackPanel> 
              <ListBox ItemsSource="{Binding WrapUps}" 
                Visibility="{Binding Path=IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}" 
                BorderThickness="0" 
                 Background="Transparent"> 
               <ListBox.ItemTemplate> 
                <DataTemplate> 
                 <StackPanel Orientation="Horizontal"> 
                  <CheckBox IsChecked="{Binding IsSelected}" Width="20" /> 
                  <TextBlock Text="{Binding FriendlyDescription}" /> 
                 </StackPanel> 
                </DataTemplate> 
               </ListBox.ItemTemplate> 
              </ListBox> 

             </StackPanel> 
            </DataTemplate> 
           </ComboBox.ItemTemplate> 
          </ComboBox> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

enter image description here

+0

你通过他们得到任何事件?可能会尝试添加'ClickMode =“按”“到CB的 –

+0

@ChrisW。请改变这个答案,我可以标记它是正确的,这允许在'DataGrid'中勾选CheckBoxes,现在唯一的问题是每次点击'ComboBox'关闭。 –

+1

Alrighty,第二部分PS我总是做这样的事情[this](http://stackoverflow.com/questions/3667203/expand-the-combobox-and-its-items-when-it-gets-focus)。 :) –

回答

1

当你有一个嵌套的控制,其父母通常会HitTestVisibility你可以泡出劫持并允许嵌套的控制,以响应它的代替正常事件它是通过使用ClickMode枚举通过ClickMode="Pressed"的属性添加到相关控件中的父项。

希望这有助于,欢呼!