如何指定将弹出任何usercontrol的自定义ListBoxItem模板

时间:2017-10-25 15:01:26

标签: c# xaml popup custom-controls listboxitem

我正在尝试编写一个自定义ListBox,它将在运行时弹出用户指定的控件。我做了一个自定义的RadioButton,当用户选择列表中的一个按钮时,我想显示一个弹出窗口

这是自定义ListBox控件的xaml。我正在尝试使用我的自定义单选按钮作为ListboxItem:

<ListBox 
    x:Class="PopUp.SystemAccessPanel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:PopUp"
    d:DataContext="{d:DesignInstance local:PanelButton}"
    mc:Ignorable="d"
    >
    <ListBox.Resources>
        <Style  TargetType="ListBoxItem">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate  TargetType="ListBoxItem">
                        <StackPanel>
                            <local:PanelButton 
                                x:Name="Button1"  
                                Foreground="{TemplateBinding Foreground}"
                                GroupName="{Binding GroupName}" 
                                ButtonText="{Binding ButtonText}"
                                Margin="0,0,0,20"
                                />
                            <Popup 
                                x:Name="PART_Popup" 
                                IsOpen="{Binding IsChecked, ElementName=Button1}"
                                PlacementTarget="{Binding ElementName=Button1}"
                                Placement="Absolute"
                                VerticalOffset="{Binding AbsoluteYPlacement}"
                                HorizontalOffset="{Binding AbsoluteXPlacement}"
                                local:BaseExtendedPopup.ClosesOnInput="True"
                                >
                                <ContentPresenter/>
                            </Popup>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.Resources>

    <ListBox.Template>
        <ControlTemplate>
            <Border 
                BorderThickness="0" 
                Padding="1,1,1,1" 
                Background="Transparent" 
                Name="theBorder" 
                SnapsToDevicePixels="True"
                >
                <ItemsPresenter 
                    SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" 
                    />
            </Border>
            <!--
            <ControlTemplate.Triggers>
                <Trigger Property="ItemsControl.IsGrouping" Value="True">
                    <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
                </Trigger>
            </ControlTemplate.Triggers>
            -->
        </ControlTemplate>
    </ListBox.Template>
</ListBox>

在这里,我想如何使用控件:

<usr:SystemAccessPanel  Foreground="Pink" Background="Aqua">
    <usr:PanelButton  
        ButtonText="What" 
        ImageWidth="50" 
        ImageHeight="50" 
        Width="40" 
        GroupName="Test" 
        AbsoluteXPlacement="900" 
        AbsoluteYPlacement="100"
        >
        <usr:UserControl2/>
    </usr:PanelButton>

    <usr:PanelButton  
        ButtonText="The Hell" 
        ImageWidth="45" 
        Height="20" Width="100" 
        GroupName="Test" 
        AbsoluteXPlacement="900" 
        AbsoluteYPlacement="100"
        >
        <usr:UserControl1/>
    </usr:PanelButton>
</usr:SystemAccessPanel>

然而,我最终得到的是一个PaneButton作为Popup = S.我需要做什么才能让我的用户控件进入弹出窗口?

0 个答案:

没有答案