1

I have listbox inside popup. How would I close popup right after I select item from list box; here is code:

 <Popup x:Name="ColorPopup" AllowsTransparency="True" 
 IsOpen="{Binding ElementName=ColorToggle, Path=IsChecked}" Placement="Bottom" StaysOpen="False" PlacementTarget="{Binding ElementName=ColorToggle}">

  <Border x:Name="DropDownBorder1" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" Margin="0, 0,5,5" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" Effect="{DynamicResource WindowShadowEffect}">
      <ListBox Name="ColorList" VerticalContentAlignment="Stretch" Margin="1, 3, 1, 3"   IsEnabled="True" Grid.Column="0" Background="Transparent" HorizontalContentAlignment="Center" SelectedItem="{Binding fColor}" SelectionMode="Single" Style="{StaticResource HorizontalListBoxStyle2}" ItemsSource="{Binding FillColors}">
      </ListBox>

   </Border>

</Popup>
4

1 回答 1

2

订阅SelectionChanged事件。

您可以在代码隐藏中执行此操作:

private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
    // ColorPopup.IsOpen = false; ?? or ColorToggle.IsChecked = false; 
}
<ListBox SelectionChanged="ListBox_SelectionChanged_1" ... />

或者如果你使用 MVVM 模式......

例如对于 MVVM-Light,它可能是这样的:

<ListBox ...>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <cmd:EventToCommand Command="{Binding Path=ClosePopupCommand}" PassEventArgsToCommand="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListBox>

或者 Prism 可能几乎相同:

 <ListBox>
      <i:Interaction.Triggers>
           <i:EventTrigger EventName="SelectionChanged">
                <prism:InvokeCommandAction Command="{Binding Path=ClosePupupCommand}" />
           </i:EventTrigger>
      </i:Interaction.Triggers>
 </ListBox> 

或者,如果您有肮脏的想法,您也可以在fColor属性设置器中关闭弹出窗口。:)

    public object fColor
    {
        get
        {
            return this.fColorField;
        }

        set
        {
            this.fColorField= value;
            IsColorToggelChecked = false;
            RaisePropertyChanged(() => this.fColor);
        }
    }
于 2013-04-25T14:19:51.793 回答