【问题标题】:TemplateBinding in Binding Path绑定路径中的模板绑定
【发布时间】:2014-10-20 07:23:42
【问题描述】:

我有一个 Custom Control,其依赖属性为 Header。在Generic.xaml 中有该控件的样式。我想设置模板某些部分的可见性,使其依赖于控件中的Header 属性。

<Style TargetType="{x:Type CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CustomControl1}">
                <Grid MinWidth="400" Focusable="False">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>

                    <TextBlock Text="{TemplateBinding Header}" 
                               Style="{DynamicResource SomeStyle}" 
                               Margin="0,0,80,0"
                               Grid.Row="0" Grid.Column="0">
                        <TextBlock.Visibility>



                            <Binding Path="{TemplateBinding Header}">

                            or alternatively

                            <Binding Path="{Binding Path=Header, RelativeSource={RelativeSource TemplatedParent}}">



                                <Binding.Converter>
                                    <converters:ValueConverterGroup>
                                        <converters:StringNullOrEmptyToBooleanConverter />
                                        <BooleanToVisibilityConverter />
                                    </converters:ValueConverterGroup>
                                </Binding.Converter>
                            </Binding>
                        </TextBlock.Visibility>
                    </TextBlock>

                   ...

这行抛出异常:

<Binding Path="{Binding Path=Header, RelativeSource={RelativeSource TemplatedParent}}">

根据异常消息,Binding 的 Path 属性内不能有 Binding(我已经知道了)xD

如果没有双重绑定,怎么能做到这一点?如何在不使用 TemplateBinding 的情况下访问自定义控件的属性?

【问题讨论】:

    标签: c# wpf binding templatebinding


    【解决方案1】:

    BindingPath 属性不是 DependencyProperty,我怀疑您可能希望它是这样的:

    <TextBlock.Visibility>
       <Binding Path="Header" RelativeSource="{RelativeSource TemplatedParent}">
              <Binding.Converter>
                  <converters:ValueConverterGroup>
                       <converters:StringNullOrEmptyToBooleanConverter />
                        <BooleanToVisibilityConverter />
                  </converters:ValueConverterGroup>
              </Binding.Converter>
       </Binding>
    </TextBlock.Visibility>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 2012-11-11
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多