Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Wpf弹出式布局_C#_Wpf_Binding - Fatal编程技术网

C# Wpf弹出式布局

C# Wpf弹出式布局,c#,wpf,binding,C#,Wpf,Binding,是否有可能将弹出窗口放在列表框中的项目旁边? 我使用MVVM,列表绑定到元素,对于一些choosen元素,我希望在项目旁边显示弹出窗口 我有元素列表,我想在单击指定的列表元素时显示弹出窗口,但弹出窗口应该显示在所选列表项的旁边 我试过这样的方法(不起作用): 我不想使用代码隐藏,只有xaml,因为您希望在单击项目时显示弹出窗口,这是否适用于您: <Popup IsOpen="{Binding Path=ShowPopup}" Placement="Mouse"> &l

是否有可能将弹出窗口放在列表框中的项目旁边? 我使用MVVM,列表绑定到元素,对于一些choosen元素,我希望在项目旁边显示弹出窗口

我有元素列表,我想在单击指定的列表元素时显示弹出窗口,但弹出窗口应该显示在所选列表项的旁边

我试过这样的方法(不起作用):



我不想使用代码隐藏,只有xaml,因为您希望在单击项目时显示弹出窗口,这是否适用于您:

<Popup  IsOpen="{Binding Path=ShowPopup}" Placement="Mouse">
     <TextBox Background="Red" Height="120" Text="Aaaaaa FUUUUUUUUUUUUU....."></TextBox>
 </Popup>

您的示例不起作用的原因很简单,因为您正在将放置目标绑定到非ui对象

PlacementTarget="{Binding ElementName=List1, Path=SelectedItem}"
在本例中,SelectedItem可能是表示列表中某个项目的模型/视图模型,因此PlacementTarget属性的用法不正确

您需要的是将PlacementTarget设置为ItemContainer(),如果没有“some”代码的帮助,这是不可能的


现在您已经知道了问题所在,有几种方法可以使代码正常工作,因此我将把它留给您。

这将在所选ListBoxItem的右侧放置一个弹出窗口

范例

<Window.Resources>
    <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />
    <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />

    <ControlTemplate x:Key="PopupListBoxItemTemplate" TargetType="ListBoxItem">
        <Border Name="Border" Padding="2" SnapsToDevicePixels="true">
            <Grid>
                <Popup Name="c_popup" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" >
                    <Border BorderBrush="Black" BorderThickness="1" CornerRadius="2.5">
                        <TextBlock Background="Wheat" Foreground="Black" Text="Aaaaaa FUUUUUUUUUUUUU....."/>
                    </Border>
                </Popup>
                <ContentPresenter />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
                <Setter TargetName="c_popup" Property="IsOpen" Value="True"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <ListBox Name="listBox"
             ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template" Value="{StaticResource PopupListBoxItemTemplate}" />
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>

很好的解决方案,但我发现了一个小错误。为了实现这一点,我必须将Setter从{StaticResource ListBoxItemTemplate}更改为StaticResource PopupListBoxItemTemplate}。
<Window.Resources>
    <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />
    <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />

    <ControlTemplate x:Key="PopupListBoxItemTemplate" TargetType="ListBoxItem">
        <Border Name="Border" Padding="2" SnapsToDevicePixels="true">
            <Grid>
                <Popup Name="c_popup" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" >
                    <Border BorderBrush="Black" BorderThickness="1" CornerRadius="2.5">
                        <TextBlock Background="Wheat" Foreground="Black" Text="Aaaaaa FUUUUUUUUUUUUU....."/>
                    </Border>
                </Popup>
                <ContentPresenter />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
                <Setter TargetName="c_popup" Property="IsOpen" Value="True"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <ListBox Name="listBox"
             ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template" Value="{StaticResource PopupListBoxItemTemplate}" />
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>