【问题标题】:XAML custom text in TextBlockTextBlock 中的 XAML 自定义文本
【发布时间】:2020-09-02 18:17:03
【问题描述】:

下午好,

我将双精度列表 (Latlng) 绑定到 ItemsControlTextBlock 我想在绑定的列表计数为 0 时创建自定义文本。使用代码我有当ItemsControlItemsSource 是该列表时,TextBlock 为空。

我做错了什么?

顺便说一句,Latlng 列表是一个类的属性。

<ItemsControl Name="icLatLng" ItemsSource="{Binding Latlng}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock FontFamily="Arial" FontSize="14">
                <TextBlock.Style>
                    <Style>
                    <Setter Property="TextBlock.Text" Value="{Binding}"></Setter>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=Items.Count}" Value="0">
                                <Setter Property="TextBlock.Text" Value="—"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

提前致谢。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    欢迎来到 SO。

    ItemsControl.ItemTemplate 是应用于控件绑定到的 Items 列表中每个元素的模板。如果没有项目开始,则不会创建,因此您在其中所做的任何事情都不会被看到。

    我怀疑您真正想做的是在列表为空时替换整个控件的外观。如果是这样,您可以使用 HasItems 属性上的 DataTrigger 将新模板应用于 ItemsControl 本身:

    <ItemsControl Name="icLatLng" ItemsSource="{Binding Latlng}">
        <ItemsControl.Style>
            <Style TargetType="{x:Type ItemsControl}" BasedOn="{StaticResource {x:Type ItemsControl}}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding HasItems, RelativeSource={RelativeSource Self}}" Value="False">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ItemsControl}">
                                    <TextBlock Text="-" />
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ItemsControl.Style>
    </ItemsControl>
    

    【讨论】:

      猜你喜欢
      • 2012-10-26
      • 2012-11-18
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多