【问题标题】:Binding within a template在模板内绑定
【发布时间】:2017-09-03 05:28:26
【问题描述】:

我有如下ControlTemplate,如下图,想避免硬编码图片的高宽,而是想绑定Image控件的高宽

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

到TextBlock的Text(TextBlock名为“ErrorText”)属性path =“Height”。图片的Height和Width都应该绑定到TextBlock的Text Height属性的Height值

有谁知道我如何做到这一点?

模板:

    <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>

【问题讨论】:

  • 我猜你真正想要的是 ErrorContent 上的 DataTrigger,当 ErrorContent 不为空时,它将图像可见性设置为可见。

标签: c# wpf


【解决方案1】:

答案的关键是引用过去的控件(在“{Binding ElementName=ErrorText, Path=ActualHeight}”中已经遇到的执行线程的控件)。所以解决方案使用网格,然后在指定引用 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 2015-07-03
    • 2013-11-13
    • 1970-01-01
    • 2017-12-25
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多