关闭WPF中的特定UserControl窗口

时间:2013-08-07 14:43:53

标签: wpf xaml c#-4.0 wpf-controls

我的问题是当点击“Mod”时它会加载UserControl,这会打开一个很好的窗口,但是当我选择一个新的客户端并重新加载页面时,UserControl窗口仍然是打开的。我想知道我是否可以做一个foreach并尝试查找UserControl是否打开然后关闭。

SPGrid也是一个UserControl。

---SPGrid XAML----
<DataGridTextColumn Header="Mod" Binding="{Binding Path=Modifiers}" MinWidth="35" MaxWidth="35" IsReadOnly="True">
<EventSetter Event="PreviewMouseUp" Handler="ModifierClick" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Modifiers}"  Value="Mod">
                                <Setter Property="Background" Value="Pink" />
                            </DataTrigger>

=============================================== ===========================================

----Modifer xaml----
<UserControl
    x:Class="test.SharedLib.Modifiers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Loaded="Window_Loaded"
    LostMouseCapture="Button_Click">

    <StackPanel
        Orientation="Vertical">
        <ItemsControl
            Name="lstMod"
            Margin="5">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel
                        Name="wrapMain"
                        Orientation="Horizontal"
                        ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                        Width="280" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border
                        Name="brd"
                        CornerRadius="1"
                        BorderThickness="1"
                        BorderBrush="Black"
                        Margin="2"
                        PreviewMouseLeftButtonDown="Border_PreviewMouseLeftButtonDown">
                        <StackPanel
                            Orientation="Horizontal">
                            <TextBlock
                                Text="{Binding CPTModifier}"
                                Name="tbMod"
                                Width="22"
                                Margin="4 0 0 0" />
                            <TextBlock
                                Text=""
                                Name="tbOrd"
                                Width="8"
                                FontSize="8"
                                Foreground="Red"
                                FontWeight="Bold" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</UserControl>

=============================================== =========================================

private void ModifierClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                TextBlock txtMod = (TextBlock)sender;
                if (cur != null && cur != txtMod)
                {
                    return;
                }

                if (pop == null)
                {
                    cur = txtMod;
                    DataRowView view = GetCurrentDataRowView();
                    Modifiers mod = new Modifiers(view["Code"].ToString(), accession);
                    mod.tbMod = txtMod;
                    pop = new Popup();
                    pop.Child = mod;
                    pop.PopupAnimation = PopupAnimation.Slide;
                    pop.StaysOpen = false;
                    pop.PlacementTarget = txtMod;
                    pop.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
                    pop.IsOpen = true;
                    pop.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    pop.HorizontalOffset = -(dataGrid0.Columns[0].ActualWidth + dataGrid0.Columns[1].ActualWidth);
                }
                else if (pop.IsOpen == false)
                {
                    pop.IsOpen = true;
                }
                else
                {
                    ((Modifiers)pop.Child).OnClickClose();
                    pop.IsOpen = false;
                    pop = null;
                    DataRowView view = GetCurrentDataRowView();
                    view["Modifiers"] = txtMod.Text;
                    cur = null;
                }
            }

0 个答案:

没有答案