C# 用户控件的动态放置目标?

C# 用户控件的动态放置目标?,c#,wpf,xaml,C#,Wpf,Xaml,所以我用他的相对视图模型构建了一个用户控件。当在组合框内单击按钮时,必须显示此控件。为了让您更好地了解,我将发布我的代码: <ItemsControl Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" HorizontalAlignment="Center" ItemsSource="{Binding MyItems}"> <ItemsControl.ItemTemplate>

所以我用他的相对视图模型构建了一个用户控件。当在组合框内单击按钮时,必须显示此控件。为了让您更好地了解,我将发布我的代码:

<ItemsControl Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" HorizontalAlignment="Center"
          ItemsSource="{Binding MyItems}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Center" Margin="7,0,10,0">
                    <Grid.Resources>
                        <CollectionViewSource x:Key="cvs" Source="{Binding CBSource}" />
                    </Grid.Resources>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="180"/>
                        <ColumnDefinition Width="180"/>
                        <ColumnDefinition Width="180"/>
                    </Grid.ColumnDefinitions>
                    <Label HorizontalContentAlignment="Center" Grid.Column="0" Content="{Binding FirstProperty}"/>
                    <Label HorizontalContentAlignment="Center" Grid.Column="1" Content="{Binding SecondProperty}"/>
                    <Label HorizontalContentAlignment="Center" Grid.Column="2" Content="{Binding ThirdProperty}"/>
                    <ComboBox HorizontalAlignment="Center" Grid.Column="2" Width="140" Visibility="{Binding HasCombobox, Converter={StaticResource BoolToVis}}">
                        <ComboBox.ItemsSource>
                            <CompositeCollection>
                                <CollectionContainer Collection="{Binding Source={StaticResource cvs}}" />
                                <ComboBoxItem>
                                    <Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Content="{x:Static prop:Resources.INSERT_BTN}" Command ="This will call CustomUserControl"></Button>
                                </ComboBoxItem>
                            </CompositeCollection>
                        </ComboBox.ItemsSource>
                    </ComboBox>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

这是我的
项目控件的模板。每当验证属性时,此控件都会显示一个组合框。因此,在我看来,我不知道我将拥有多少个
ComboBox
,它们将被放置在哪里(所有这些
ItemsControl
的组合创建了一种网格)。我的目标是显示一个带有可变放置目标的小视图(它必须在调用它的
组合框附近弹出)


要求:我需要一种方法将我的
CustomUserControl.xaml
放置在我上面描述的
ItemControl
中,并带有一个动态放置目标。调用此控件的按钮,将有一个
ICommand
专用于执行例程来设置和显示CustomUserControl

您的要求有点不清楚,但您可以使用
弹出式
元素,并将其
放置目标
绑定到打开时弹出式
所定位的元素:

然后,您可以在
弹出窗口中使用
ContentControl
,并将其
Content
属性绑定到一个属性,该属性返回一个视图模型(或用户控件),该视图模型定义要在弹出窗口中显示的内容。大概是这样的:

<Button x:Name="btn" Content="Button" />
<Popup IsOpen="True" StaysOpen="True"
       PlacementTarget="{Binding ElementName=btn}" 
       Placement="Top">
    <Border BorderThickness="1" Width="100" Height="100">
        <ContentControl Content="{Binding TheControlProperty}" />
    </Border>
</Popup>


是否要在
组合框附近放置
CustomUserControl
来显示
弹出窗口
?@Maxim我从未使用过
弹出窗口
,因此我不知道它是否适合我的情况。我只知道
CustomUserControl
是一个视图,其视图模型必须显示在调用的组合框附近it@Maxim另外,我的问题是,在弹出窗口中,我是否可以在每次属性IsOpened=true时生成内容?因为CustomUserControl是一个具有不同内容的视图,根据哪个combobox按钮启动命令动态内容来自何处?@mm8“动态内容”是一个视图,可以通过以下方式作为控件的内容放置:。它将由一个按钮(每个按钮位于不同的组合框中)使用命令生成,可以直接作为弹出窗口的内容显示在视图中?如果您希望此视图是动态的,则控件属性应该返回视图。显然,如果希望能够动态更新视图,就不能在XAML标记中硬编码视图。完美。这就是我需要的。你们知道为什么吗,若我点击弹出窗口的任何一点,它就会消失?若你们有其他问题,请问另一个问题。