如何在控件背景中设置文本(how to set text in background of control)
我想在控件背景中设置一些默认文本(例如TreeView,ListView,GridView等)。 当项目控件不为空时,项目中没有项目控制和隐藏时显示文本。
我想它是这样的:
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 control and hides when items control is not empty.
I imagine it like 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>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>如何在控件背景中设置文本(how to set text in background of control)我想在控件背景中设置一些默认文本(例如TreeView,ListView,GridView等)。 当项目控件不为空时,项目中没有项目控制和隐藏时显示文本。
我想它是这样的:
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 control and hides when items control is not empty.
I imagine it like 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>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>
发布评论