Customize Content Management Background

<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>

As you can see from the above code, ContentControl sets its content by binding to the ViewModel Screen property. The Screen property will return an instance of AViewModel or BViewModel depending on some state. The problem is that when UserControl loads onto the screen, the Screen property is NULL, so there is no content yet. At the moment, I would like to set some background for ContentControl, but I cannot find a way how to do this? Background = "Yellow" does nothing ...

Any ideas on how to set the ContentControl background? This backgound should always be applied, even when the content displays AView or Biew, or null.

+5
source share
3 answers

ContentControl Border

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

, UserControl, ContentControl, Background UserControl. Border.

+5

- :

 <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>
+1

try something like this in 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>
0
source

All Articles