简体   繁体   English

如何将内容动态放入控件中

[英]How do I put Content dynamically in a control

I have two almost identical GUI structurs in XAML, the difference is in one StackPanel that have an Image and a Rectangle in respective structure, the StackPanel contains other content as well. 我在XAML中有两个几乎相同的GUI结构,区别在于一个StackPanel在各自的结构中具有一个Image和一个Rectangle,而StackPanel也包含其他内容。 I want to only write the hole structure once and have a dynamic resource where a put the Image or Rectangle. 我只想编写一次孔结构,并在其中放置Image或Rectangle的地方有一个动态资源。 The code will basicly be like: 该代码基本上是这样的:

<Style x:Key="myGrid" TargetType="{x:Type Grid}">
    <!-- Many nested controls -->
        <StackPanel>
            <!-- Some differnt Controls -->
            <Object Value="{DynamicResource someDynamicItem}" />
        <StackPanel/>
</Style>
<Style x:Key="myGridWithImage" BasedOn="{StaticResource myGrid}" TargetType="{x:Type Grid}">
    <Style.Resources>
        <Image x:Key="someDynamicItem" />
    </Style.Resources>
</Style>
<Style x:Key="myGridWithRectangle" BasedOn="{StaticResource myGrid}" TargetType="{x:Type Grid}">
    <Style.Resources>
        <Rectangle x:Key="someDynamicItem" />
    </Style.Resources>
</Style>

Is it possibly to have any kind of placeholder (instead of the Object-Tag) that could be dynamically assigned in this way? 是否可以通过这种方式动态分配任何类型的占位符(而不是Object-Tag)?

You can put a content control in your grid and then can have its content set based on some condition, if that condition is not met you can either choose to not set the content or may be make its visibility collapsed. 您可以在网格中放置一个内容控件,然后根据某种条件设置其内容,如果不满足该条件,则可以选择不设置内容,或者使其可见性崩溃。

I recommend content control over stack panel in you case, so that in future if you have a requirement to fit in something else you just need to provide a new content to content control. 在这种情况下,我建议对堆栈面板进行内容控制,以便将来如果您需要适应其他内容,则只需为内容控制提供新内容即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在wcf中动态地向选项卡控件添加内容? - How do you add content to a tab control dynamically in wcf? 在WPF中,如何将UserControl的Content属性绑定到内部控件 - In WPF how do I bind a Content Property of the UserControl to the internal control 如何将用户控件添加到Xamarin表单的内容页面? - How do I add a User control to Content Page in Xamarin Forms? 在充当容器的WPF控件中,如何放置内容? - In a WPF control that acts as a container, how do I place the content? 如何使内容控件可被多个WPF窗口访问? - How do I make a content control accessible to multiple WPF windows? 如何获取DataGridColumnHeadersPresenter控件模板的标题内容? - How do I get the header content for a DataGridColumnHeadersPresenter control template? 如何在 WPF 中为用户控件允许多个项目/内容? - How do I allow multiple items/content for a user control in WPF? 如何在WPF中的按钮中动态设置多个内容 - How Do I Set Multiple Content Dynamically in a Button in WPF 如何从代码隐藏中在用户控件的内容控件中的数据模板中启动故事板? - How do I start a Storyboard in a Data Template in a Content Control in a User Control from codebehind? 如何在窗口应用程序中动态添加用户控件中的内容? - how to add content in user control dynamically in window application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM