WPF DataTemplate内重写BorderBrush,VisualBrush内数据源绑定提示绑定失败

定义DataTemplate 数据模板文件,内容如下

xml 复制代码
<DataTemplate x:Key="{DataTemplateKey {x:Type VM:TemplateListVM}}" DataType="{x:Type VM:TemplateListVM}">
<Grid Margin="0" Grid.Row="3" Height="50" VerticalAlignment="Top" Visibility="{Binding ShowNoMore, Converter={StaticResource BooleanToVisibilityConverter}}">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="auto" />
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
          <Border Grid.Column="0" Height="1" Margin="60,0,10,0" BorderThickness="1" Name="LeftBorder">
            <Border.BorderBrush>
              <VisualBrush>
                <VisualBrush.Visual>
                  <Rectangle StrokeDashArray="4 4" Stroke="Gray" StrokeThickness="1" 
                             Width="{Binding ElementName=LeftBorder, Path=ActualWidth}" 
                             Height="2" />
                </VisualBrush.Visual>
              </VisualBrush>
            </Border.BorderBrush>
          </Border>
          <TextBlock Grid.Column="1" VerticalAlignment="Center" Text="{x:Static loc:MainWindowLoc.NoMoreData}" Style="{StaticResource BodyStyleWhite}" Foreground="{StaticResource BorderGrayBrush}" />
          <Border Grid.Column="2" Height="1" Margin="10,0,60,0" BorderThickness="1">
            <Border.BorderBrush>
              <VisualBrush>
                <VisualBrush.Visual>
                  <Rectangle StrokeDashArray="4 4" Stroke="Gray" StrokeThickness="1" 
                             Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualWidth}" 
                             Height="2" />
                </VisualBrush.Visual>
              </VisualBrush>
            </Border.BorderBrush>
          </Border>       
      </Grid>
</DataTemplate>

虽然显示效果没有问题,但程序运行时两个Width的绑定均会提示绑定失败:

ActualWidth Rectangle.Width Double 找不到源: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.Border', AncestorLevel='1'

或者是找不到LeftBorder。

但如果直接写在userControl里面是没有任何提示的,研究了好久,发现在DataTemplate 需要这样写:

xml 复制代码
 <Grid Margin="0" Grid.Row="3" Height="50" VerticalAlignment="Top" Visibility="{Binding ShowNoMore, Converter={StaticResource BooleanToVisibilityConverter}}">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
          </Grid.ColumnDefinitions>
          
          <Grid.Resources>
              <VisualBrush x:Key="leftBorderBrush">
                <VisualBrush.Visual>
                  <Rectangle StrokeDashArray="4 4" Stroke="Gray" StrokeThickness="1" Width="{Binding ElementName=LeftBorder, Path=ActualWidth}"  Height="2" />
                </VisualBrush.Visual>
              </VisualBrush>  
              <VisualBrush x:Key="RightBorderBrush">
                <VisualBrush.Visual>
                  <Rectangle StrokeDashArray="4 4" Stroke="Gray" StrokeThickness="1"  Width="{Binding ElementName=RightBorder , Path=ActualWidth}" Height="2" />
                </VisualBrush.Visual>
              </VisualBrush>            
          </Grid.Resources>
          
          <Border Grid.Column="0" Height="1" Margin="60,0,10,0" BorderThickness="1" Name="LeftBorder" BorderBrush="{StaticResource leftBorderBrush}" />
          <TextBlock Grid.Column="1" VerticalAlignment="Center" Text="{x:Static loc:MainWindowLoc.NoMoreData}" Style="{StaticResource BodyStyleWhite}" Foreground="{StaticResource BorderGrayBrush}" />
          <Border Grid.Column="2" Height="1" Margin="10,0,60,0" BorderThickness="1" Name="RightBorder"  BorderBrush="{StaticResource RightBorderBrush}"/>

        </Grid>           
	  </Grid>

刚使用wpf做正式项目,暂不清楚两种写法的区别。

相关推荐
步、步、为营25 分钟前
解锁 C# 与 LiteDB 嵌入式 NoSQL 数据库
数据库·c#
petunsecn30 分钟前
EFCore HasDefaultValueSql
c#·.net core
三天不学习3 小时前
【update 更新数据语法合集】.NET开源ORM框架 SqlSugar 系列
数据库·后端·c#·.net·orm·sqlsugar
玉面小君3 小时前
C# 获取当前运行路径的6种实用方法
microsoft·c#
pchmi16 小时前
C# OpenCV机器视觉:骨架细化
开发语言·opencv·c#
W Y17 小时前
【Unity-和WPF结合的优势】
unity·游戏引擎·wpf
范纹杉想快点毕业17 小时前
XML通过HTTP POST 请求发送到指定的 API 地址,进行数据回传
xml·c语言·开发语言·数据结构·c++·python·c#
Ma_si17 小时前
python 如何调整word 文档页眉页脚
python·c#·word
Libby博仙18 小时前
.net core 为什么使用 null!
javascript·c#·asp.net·.netcore
白白白白纸呀18 小时前
ADO.NET知识总结6---SqlDataAdapter桥接器
开发语言·c#·.net