繁体   English   中英

与模板多重绑定时,WPF标签为空

[英]WPF Label is blank when multibinding with Template

我有一个带有模板的标签,我正在尝试进行多重绑定,但是当与模板结合使用时,该标签为空

以下是标签的代码

        <Label Template="{StaticResource SmallHeaderTemplate}" Margin="0,10,5,0" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"  >
            <Label.Style>
                <Style>
                    <Setter Property="TextBlock.Text">
                        <Setter.Value>
                            <MultiBinding StringFormat="{}{0} {1}">
                                <Binding Source="{x:Static res:Strings.Val1}"></Binding>
                                <Binding Source="{x:Static res:Strings.Val2}"></Binding>
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>

                </Style>
            </Label.Style>
        </Label>

和模板是

   <ControlTemplate x:Key="SmallHeaderTemplate" TargetType="{x:Type ContentControl}">
        <StackPanel SnapsToDevicePixels="True">
            <TextBlock Foreground="#FF0072C6" FontSize="20"  FontWeight="Regular" FontFamily="Segoe UI Light"  Text="{TemplateBinding Content}" Margin="2,7,2,5" SnapsToDevicePixels="True" />
            <Border Height="1" Margin="0,0,0,5">
                <Border.Background>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                        <GradientStop Offset="0" Color="#FFFCFCFC" />
                        <GradientStop Offset="0.5" Color="#FFD7D7D7" />
                        <GradientStop Offset="1" Color="#FFFCFCFC" />
                    </LinearGradientBrush>
                </Border.Background>
            </Border>
        </StackPanel>
    </ControlTemplate>

我也尝试了标签的跟随,但没有运气

    <Label Template="{StaticResource SmallHeaderTemplate}" Margin="0,10,5,0" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"  >
            <Label.Content>
                <TextBlock>
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} {1}">
                        <Binding Source="{x:Static res:Strings.Val1}"></Binding>
                        <Binding Source="{x:Static res:Strings.Val2}"></Binding>
                    </MultiBinding>
                        </TextBlock.Text>
                </TextBlock>
            </Label.Content>
        </Label>

谁能给我一个提示,为什么它不起作用?

您的文本块是绑定到内容(来自标签)的模板。 标签内容永远不会设置(忽略您将其设置为TextBlock的第三个代码块-不是字符串,而是实际的TextBlock)。

因此,设置标签内容。 您只能将StringFormat与字符串一起使用(Label.Content不是字符串)。 因此,请使用转换器进行格式化(在这种情况下为MultiBinding)。

这是一个工作示例:XAML:

<Window x:Class="WpfApp34.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:res="clr-namespace:WpfApp34.Statics"
    xmlns:local="clr-namespace:WpfApp34"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.Resources>
    <local:MyConverter x:Key="MyConverter" />
    <ControlTemplate x:Key="SmallHeaderTemplate" TargetType="{x:Type ContentControl}">
        <StackPanel SnapsToDevicePixels="True">
            <TextBlock Foreground="#FF0072C6" FontSize="20"  FontWeight="Regular" FontFamily="Segoe UI Light"  Text="{TemplateBinding Content}" Margin="2,7,2,5" SnapsToDevicePixels="True" />
            <Border Height="1" Margin="0,0,0,5">
                <Border.Background>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                        <GradientStop Offset="0" Color="#FFFCFCFC" />
                        <GradientStop Offset="0.5" Color="#FFD7D7D7" />
                        <GradientStop Offset="1" Color="#FFFCFCFC" />
                    </LinearGradientBrush>
                </Border.Background>
            </Border>
        </StackPanel>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <Label Template="{StaticResource SmallHeaderTemplate}" Margin="0,10,5,0" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">
        <Label.Content>
            <MultiBinding Converter="{StaticResource MyConverter}" ConverterParameter="{}{0} {1}">
                <Binding Source="{x:Static res:Strings.Val1}" />
                <Binding Source="{x:Static res:Strings.Val2}" />
            </MultiBinding>
        </Label.Content>
    </Label>
</Grid>

码:

    using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace WpfApp34
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MyConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            return string.Format(parameter as string, values);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    namespace Statics
    {
        public class Strings
        {
            public static string Val1 { get; set; } = "Val_1";
            public static string Val2 { get; set; } = "Val_2";
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM