WPF Border DesiredHeight

Jos*_*ant 10 data-binding wpf controltemplate

以下Microsoft示例代码包含以下内容:

<Grid>
...     
  <Border Name="Content" ... >
...     
  </Border>
</Grid>
<ControlTemplate.Triggers>
  <Trigger Property="IsExpanded" Value="True">
     <Setter TargetName="ContentRow" Property="Height"
             Value="{Binding ElementName=Content,Path=DesiredHeight}" />
  </Trigger>
...
</ControlTemplate.Triggers>
Run Code Online (Sandbox Code Playgroud)

但是,运行时,此代码会生成以下数据绑定错误:

System.Windows.Data Error: 39 : BindingExpression path error: 'DesiredHeight' property not found on 'object' ''Border' (Name='Content')'. BindingExpression:Path=DesiredHeight; DataItem='Border' (Name='Content'); target element is 'RowDefinition' (HashCode=2034711); target property is 'Height' (type 'GridLength')
Run Code Online (Sandbox Code Playgroud)

尽管有这个错误,代码仍能正常工作.我查看了文档并且DesiredHeight似乎不是其成员Border.任何人都可以解释DesiredHeight来自哪里?另外,有没有办法解决/抑制此错误,所以我的程序输出是干净的?

Car*_*rlo 8

您可以在应用程序的代码部分中看到该属性

编辑:

Border content = new Border();
int desiredHeight = content.DesiredSize.Height;
int desiredWidth = content.DesiredSize.Width;
Run Code Online (Sandbox Code Playgroud)

要解决此问题,请尝试将其绑定到Height属性,因为DesiredHeight似乎在Border控件的XAML标记中不可用.


And*_*ell 6

有同样的问题。在自定义Expander中使用自定义ComboBox。以上都不适合我,绑定Height破坏了 的功能Expander,使用 aStackPanel也破坏了每个组中项目的显示。我发现:

<Setter TargetName="ContentRow" Property="Height" Value="Auto"/>
Run Code Online (Sandbox Code Playgroud)