2013-03-30 30 views
5
<UserControl .....> 
    <DataTemplate DataType="{x:Type vm:AViewModel}"> 
    <vw:AView /> 
    </DataTemplate> 

    <DataTemplate DataType="{x:Type vm:BViewModel}"> 
    <vw:BView /> 
    </DataTemplate> 

    <ContentControl x:Name="chartScreen" Content="{Binding Screen}" Background="Yellow" /> 
</UserControl> 

正如您从上面的代码可以看到的,ContentControl通过绑定到ViewModel的Screen属性来设置其内容。屏幕属性将根据某些条件返回AViewModel或BViewModel的实例。问题是,当UserControl在屏幕上加载时,Screen属性为空,因此没有设置内容。在这一点上,我想为ContentControl设置一些背景,但我找不到如何做到这一点的方法?背景=“黄色”什么都不做......设置内容控件的背景

任何想法如何设置ContentControl的背景?即使内容显示AView或Biew,也应该始终应用此背景,或者为null。

回答

5

只是包装你的ContentControlBorder

<Border Background="Yellow"> 
    <ContentControl x:Name="chartScreen" 
        Content="{Binding Screen}" /> 
</Border> 

如果你在你的UserControl有是你的ContentControl,刚刚成立的UserControl本身Background。这将会删除额外的Border

1

尝试这样的事:

<ContentControl x:Name="chartScreen" Content="{Binding Screen}" Background="Yellow"> 
     <ContentControl.Triggers>  
      <Trigger Property="Content" Value="{x:Null}"> 
       <Trigger.Value> 
        <Border Background="Yellow"/> 
       </Trigger.Value> 
      </Trigger> 
     </ContentControl.Triggers>  
</ContentControl> 
+0

我喜欢@薇薇在回顾答案.. –

+0

我将标志着你回答是有帮助的,虽然它不这并不能回答这个问题。如果您阅读最后一部分的句子,则应始终应用背景,而不是仅当内容为空时。 – Goran

+0

oo ..我错过了那部分。 –

0

尝试这样的事情在WPF:

<ContentControl> 
     <ContentControl.Style> 
     <Style TargetType="ContentControl"> 
      <Style.Triggers> 
      <DataTrigger Binding="{Binding Content}" Value="{x:Null}"> 
       <Setter Property="Content"> 
       <Setter.Value> 
        <Rectangle Width="100" Height="100" Fill="Blue" /> 
       </Setter.Value> 
       </Setter> 
      </DataTrigger> 
      </Style.Triggers> 
     </Style> 
     </ContentControl.Style> 
</ContentControl>