wpf 设置内容控件背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15724208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:23:50  来源:igfitidea点击:

Setting background of content control

wpfwpf-controls

提问by Goran

<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 above code, ContentControl is setting its content through binding to ViewModel's Screen property. Screen property will return an instance of AViewModel or BViewModel, depending on some condition. The problem is, when UserControl is loaded on screen, Screen property is null, so there is no content set yet. At this point, I would like to set some background for the ContentControl, but I cannot find a way how to do this? Background="Yellow" does nothing...

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

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

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

采纳答案by Viv

Just wrap your ContentControlin a Border

把你的包裹ContentControl在一个Border

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

If all you have in your UserControlis your ContentControl, just set the Backgroundon the UserControlitself. That would remove the extra Borderas well.

如果你在有你的UserControl就是你的ContentControl,只是在设置BackgroundUserControl本身。这也将删除额外的Border

回答by eran otzap

try something like this :

尝试这样的事情:

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

回答by juFo

try something like this in WPF:

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