[英]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.