【问题标题】:WPF datagrid with combobox popup automatically close带有组合框弹出窗口的 WPF 数据网格自动关闭
【发布时间】:2015-04-20 09:24:40
【问题描述】:

我有问题。 我希望为我的数据网格的每一行创建一个组合框,该组合框打开一个弹出窗口以过滤一些参数。 我设法使我的组件成为“组合框弹出过滤器”。 当我在标准用户控件中使用“组合框弹出过滤器”时,我没有问题。 当我想在数据网格中连续实现时遇到问题,当我点击弹出窗口打开时,但是当我想在弹出窗口内单击以访问过滤器设置时,弹出窗口会自动关闭,我的组合框不可用。

代码数据网格:

代码数据网格

<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");
}

【问题讨论】:

    标签: c# .net wpf datagrid popup


    【解决方案1】:

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

    【讨论】:

    • 我已经通知了 stayopen 属性为 true 但没有任何变化。我觉得问题来自 DataGrid,而不是我的组件,因为当我在 DataGrid 中使用它时,它工作得很好。
    猜你喜欢
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多