在模板中绑定

时间:2017-09-03 05:28:26

标签: c# wpf

我有以下ControlTemplate,如下所示,我想避免硬编码图像的高度和宽度,而是想要绑定图像控件的高度和宽度

    <Image Source="/Rotate.Pictures;component/Images/error.png"
       Stretch="Uniform"
       HorizontalAlignment="Stretch"
       VerticalAlignment="Stretch"
       Height="14"
       Width="14"/>

到TextBlock的文本(TextBlock名为&#34; ErrorText&#34;)属性路径=&#34;高度&#34;。图像的高度和宽度都应绑定到TextBlock的文本高度属性的高度值

有谁知道我怎么能做到这一点?

模板:

    <Grid IsSharedSizeScope="True">
    <Grid.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <StackPanel>
                            <AdornedElementPlaceholder>
                                <Border BorderBrush="Red" BorderThickness="2"/>
                            </AdornedElementPlaceholder>
                            <ItemsControl ItemsSource="{Binding}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Image Source="/Rotate.Pictures;component/Images/error.png" Stretch="Uniform" HorizontalAlignment="Stretch"
                                                   VerticalAlignment="Stretch"
                                                   Height="14"
                                                   Width="14"/>
                                            <TextBlock x:Name="ErrorText" Text="{Binding ErrorContent}" Foreground="Red"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>

1 个答案:

答案 0 :(得分:0)

答案的关键是引用过去的控件(控制执行线程已经在&#34; {Binding ElementName = ErrorText,Path = ActualHeight}&#34;中)。因此,解决方案使用网格,然后在指定引用Grid.Column 1中的ElementName的Grid.Column 0之前指定Grid.Column 1。

        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <StackPanel>
                            <AdornedElementPlaceholder>
                                <Border BorderBrush="Red" BorderThickness="2"/>
                            </AdornedElementPlaceholder>
                            <ItemsControl ItemsSource="{Binding}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>
                                            <TextBlock x:Name="ErrorText" Grid.Column="1" Text="{Binding ErrorContent}" Foreground="Red"/>
                                            <Image Grid.Column="0" Source="/Rotate.Pictures;component/Images/error.png"
                                                   Height="{Binding ElementName=ErrorText, Path=ActualHeight}"
                                                   Width="{Binding ElementName=ErrorText, Path=ActualHeight}"/>
                                        </Grid>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>