how to set text in background of control
I want to set some default text in background of control (e.g. TreeView, ListView, GridView etc). Text is shown when no item in items c开发者_运维技巧ontrol and hides when items control is not empty.
I imagine it like this:
i use the following style for this.
<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>
There is likely a .Background
property on the class. You can make an element out of it like:
<Button.Background>
<!-- content -->
</Button.Background>
This will allow you to place content inside it.
Also you can use a VisualBrush if you need a more complex background. You can create a VisualBrush out of any control in WPF. If you want a Brush with Labels in a Grid it can be done.
精彩评论