2011-05-20 19 views
1

我想在控件背景(例如TreeView,ListView,GridView等)中設置一些默認文本。當項目中沒有項目控制並且當項目控件不爲空時隱藏文本。如何在控件背景中設置文本

我想它是這樣的: enter image description here

回答

4

我對此使用以下樣式。

<Style x:Key="{x:Type ItemsControl}" TargetType="{x:Type ItemsControl}"> 
    <Setter Property="Background" Value="Transparent"/> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource Self}}" Value="0"> 
      <Setter Property="Background"> 
       <Setter.Value> 
        <VisualBrush Stretch="None"> 
         <VisualBrush.Visual> 
          <TextBlock Text="No Data" 
             FontFamily="Consolas" 
             FontSize="16"/> 
         </VisualBrush.Visual> 
        </VisualBrush> 
       </Setter.Value> 
      </Setter> 
     </DataTrigger> 
     <DataTrigger Binding="{Binding Items, RelativeSource={RelativeSource Self}}" Value="{x:Null}"> 
      <Setter Property="Background"> 
       <Setter.Value> 
        <VisualBrush Stretch="None"> 
         <VisualBrush.Visual> 
          <TextBlock Text="No Data" 
             FontFamily="Consolas" 
             FontSize="16"/> 
         </VisualBrush.Visual> 
        </VisualBrush> 
       </Setter.Value> 
      </Setter> 
     </DataTrigger> 
     <Trigger Property="IsGrouping" Value="true"> 
      <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
<Style x:Key="{x:Type DataGrid}" TargetType="{x:Type DataGrid}" BasedOn="{StaticResource {x:Type ItemsControl}}"> 
</Style> 
<Style x:Key="{x:Type TreeView}" TargetType="{x:Type TreeView}" BasedOn="{StaticResource {x:Type ItemsControl}}"> 
</Style> 
+0

謝謝,perfecto! – karabara 2011-05-20 09:43:37

0

有可能在類.Background屬性。您可以辨認出它的一個元素,如:

<Button.Background> 
    <!-- content --> 
</Button.Background> 

這將允許你把它裏面的內容。

0

如果您需要更復雜的背景,也可以使用VisualBrush。您可以使用WPF中的任何控件創建VisualBrush。如果你想在網格中使用標籤刷機,可以完成。