WPF datagrid与combobox弹出窗口自动关闭

时间:2015-04-20 09:24:41

标签: c# .net wpf datagrid popup

我有问题。 我希望我的datagrid的每一行创建一个组合框,打开一个弹出窗口,以便能够过滤一些参数。 我设法制作了我的组件"组合框弹出过滤器"。 当我使用" combobox popu过滤器"在标准用户控件中我没有问题。 当我想在数据网格中连续实现当我点击弹出窗口打开但是当我想在弹出窗口中单击以访问过滤器设置时,我有一个问题,弹出窗口会自动关闭,我的组合框不可用。

代码datagrid:

  

代码datagrid

<DataTemplate x:Key="SalleCellEditingTemplate">
<booking:BookingComboBoxSalleFilterPane ItemsSource="{Binding MyRoomRow.SalleItemsFiltered, Mode=OneTime}" SelectedItem="{Binding SelectedSalleItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" />
</DataTemplate>
  

带弹出窗口的XAML组合框

<UserControl x:Name="MyPane"
             x:Class="GTS.Core.UI.Booking.BookingComboBoxSalleFilterPane"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Focusable="False">
  <UserControl.Resources>
    <HierarchicalDataTemplate x:Key="ItemTemplate"
                              ItemsSource="{Binding Path=Children}">
      <TextBlock VerticalAlignment="Center"
                 HorizontalAlignment="Left"
                 FontSize="12"
                 Text="{Binding Path=Libelle, Mode=OneWay}" />
    </HierarchicalDataTemplate>
  </UserControl.Resources>
  <Grid x:Name="LayoutRoot"
        Focusable="False"
        MaxWidth="400">
    <Border Background="White"
            BorderBrush="{DynamicResource BorderControlBrush}"
            BorderThickness="1"
            Focusable="False"
            MinHeight="24"
            VerticalAlignment="Top">
      <Grid>
        <TextBlock x:Name="MyTextBox"
                   Background="White"
                   Foreground="{DynamicResource TextBrush}"
                   FontSize="12"
                   Focusable="False"
                   MinHeight="24"
                   TextTrimming="CharacterEllipsis"
                   VerticalAlignment="Top"
                   Padding="2,3,24,0" />
        <ToggleButton x:Name="MyToggleButton"
                      Height="{Binding Path=ActualHeight, ElementName=MyTextBox}"
                      IsChecked="{Binding Path=IsOpen, ElementName=MyPopup, Mode=TwoWay}"
                      TabIndex="0"
                      Template="{DynamicResource ComboBoxToggleButton}"
                      VerticalAlignment="Top" />
      </Grid>
    </Border>
    <Popup x:Name="MyPopup"
           AllowsTransparency="True"
           MinWidth="{Binding Path=ActualWidth, ElementName=MyToggleButton}"
           Placement="Center"
           PlacementTarget="{Binding ElementName=MyToggleButton}"
           PopupAnimation="Fade"
           MinHeight="{Binding ElementName=MyTextBox, Path=ActualHeight }"
           StaysOpen="False">
      <!--PlacementTarget="{Binding Path=MyToggleButton}"-->
      <Border Background="Red"
              BorderBrush="{DynamicResource BorderControlBrush}"
              BorderThickness="1">
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="5" />
            <RowDefinition Height="30" />
            <RowDefinition Height="5" />
            <RowDefinition Height="*" />
          </Grid.RowDefinitions>



<TreeComboBox x:Name="MyComboBox"
                        DisplayMemberPath="Libelle"
                        ItemsSource="{Binding ItemsParent, Mode=OneTime, ElementName=MyPane}"
                        SelectionChanged="MyComboBox_SelectionChanged"
                        VerticalAlignment="Center"/>
          <!--SelectedItem="{Binding ItemsParentSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"-->

          <TextBox Grid.Row="2"
                   ToolTip="Recherche..."
                   VerticalContentAlignment="Center"
                   Background="White"
                   TextChanged="TextBoxBase_OnTextChanged">
            <TextBox.Style>
              <Style TargetType="TextBox"
                     xmlns:sys="clr-namespace:System;assembly=mscorlib">
                <Style.Resources>
                  <VisualBrush x:Key="CueBannerBrush"
                               AlignmentX="Left"
                               AlignmentY="Center"
                               Stretch="None">
                    <VisualBrush.Visual>
                      <Label Content="Recherche..."
                             Foreground="LightGray" />
                    </VisualBrush.Visual>
                  </VisualBrush>
                </Style.Resources>
                <Style.Triggers>
                  <Trigger Property="Text"
                           Value="{x:Static sys:String.Empty}" >
                    <Setter Property="Background"
                            Value="{StaticResource CueBannerBrush}" />
                  </Trigger>
                  <Trigger Property="Text"
                           Value="{x:Null}">
                    <Setter Property="Background"
                            Value="{StaticResource CueBannerBrush}" />
                  </Trigger>
                  <Trigger Property="IsKeyboardFocused"
                           Value="True">
                    <Setter Property="Background"
                            Value="White" />
                  </Trigger>
                </Style.Triggers>
              </Style>
            </TextBox.Style>
          </TextBox>

          <RadTreeView x:Name="MyRadTreeView"
                       BorderBrush="Transparent"
                       Grid.Row="4"
                       ItemsSource="{Binding Path=ItemsSource, ElementName=MyPane}"
                       ItemTemplate="{StaticResource ItemTemplate}"
                       SelectedValuePath="Key"
                       CanSelectParentNode="False"
                       MaxHeight="200"/>
        </Grid>
      </Border>
    </Popup>
  </Grid>
</UserControl>
  

带弹出窗口的代码隐藏组合框:

public BookingComboBoxSalleFilterPane()
{
    InitializeComponent();
}


/// <summary>
///     Identifies the dependency of the ItemsSource property.
/// </summary>
public new static readonly DependencyProperty ItemsSourceProperty =
    DependencyProperty.Register("ItemsSource", typeof(List<SalleItem>), typeof(BookingComboBoxSalleFilterPane), new UIPropertyMetadata(OnItemsSourceChanged));
/// <summary>
///     OnItemsSourceChanged event handler.
/// </summary>
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    if (e.NewValue == e.OldValue || Equals(e.NewValue, e.OldValue)) return;

    var bookingComboBoxSalleFilterPane = d as BookingComboBoxSalleFilterPane;
    if (bookingComboBoxSalleFilterPane == null) return;
    var dataContext = bookingComboBoxSalleFilterPane.DataContext as BookingRoomInfosDetails;
    if (dataContext == null) return;

    bookingComboBoxSalleFilterPane.MyTextBox.Text = (bookingComboBoxSalleFilterPane.MyRadTreeView.SelectedItem == null) ? null : bookingComboBoxSalleFilterPane.MyRadTreeView.SelectedItem.ToString();

    if (dataContext.MyRoomRow != null)
        bookingComboBoxSalleFilterPane.ItemsParent = dataContext.MyRoomRow.SalleItemsFiltered.FindAll(item => item.Children != null && item.Children.Any());
}

/// <summary>
///     Gets or sets ItemsSource.
/// </summary>
public List<SalleItem> ItemsSource
{
    get { return (List<SalleItem>)GetValue(ItemsSourceProperty); }
    set { SetValue(ItemsSourceProperty, value); }
}

/// <summary>
///     Identifies the dependency of the ItemsParent property.
/// </summary>
public static readonly DependencyProperty ItemsParentProperty =
    DependencyProperty.Register("ItemsParent", typeof(List<SalleItem>), typeof(BookingComboBoxSalleFilterPane));
/// <summary>
///     Gets or setsItemsParent.
/// </summary>
public List<SalleItem> ItemsParent
{
    get { return (List<SalleItem>)GetValue(ItemsParentProperty); }
    set { SetValue(ItemsParentProperty, value); }
}

/// <summary>
///     Identifies the dependency of the SelectedItem property.
/// </summary>
public static readonly DependencyProperty SelectedItemProperty =
    DependencyProperty.Register("SelectedItem", typeof(SalleItem), typeof(BookingComboBoxSalleFilterPane));
/// <summary>
///     Gets or sets SelectedItem.
/// </summary>
public SalleItem SelectedItem
{
    get { return (SalleItem)GetValue(SelectedItemProperty); }
    set { SetValue(SelectedItemProperty, value); }
}


private void MyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    System.Windows.MessageBox.Show("SelectionChanged");
}

private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
    System.Windows.MessageBox.Show("OnTextChanged");
}

1 个答案:

答案 0 :(得分:0)

您将StaysOpen属性设置为false,尝试将其更改为true,然后在设置过滤器后关闭弹出窗口的事件