gpt4 book ai didi

c# - ContentControl 中的 UserControl

转载 作者:太空狗 更新时间:2023-10-30 00:03:13 24 4
gpt4 key购买 nike

是否可以将一些 UserControl 插入到 ContentControl 中?

但我需要动态决定我需要插入哪个 UserControl(就像使用 DataTemplateSelector)。

最佳答案

这是可能的。您需要有一个 ContentControl 让我们这样说:

<ContentControl Name="ContentMain"  Width="Auto" Opacity="1" Background="Transparent" ></ContentControl>

然后你需要像这两个一样拥有不同的 UserControl:

<UserControl x:Class="MyNamespace.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" >
<Grid Margin="5,5,5,10" >
<Label Name="labelContentOne" VerticalAlignment="Top" FontStretch="Expanded" />

</Grid>

<UserControl x:Class="MyNamespace.UserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" >
<Grid Margin="5,5,5,10" >
<Label Name="labelContentTwo" VerticalAlignment="Top" FontStretch="Expanded" />

</Grid>

如果您想动态更改它们,您只需以编程方式更改 ContentMain ContentControl 的内容:

// Initialize the content
UserControl1 u1 = new UserControl1();
ContentMain.Content = u1;


// Let's say it changes on a button click (for example)
private void ButtonChangeContent_Click(object sender, RoutedEventArgs e)
{
UserControl2 u2 = new UserControl2();
ContentMain.Content = u2;
}

或多或少就是这个想法……;)

关于c# - ContentControl 中的 UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14506374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com