代码之家  ›  专栏  ›  技术社区  ›  Joseph Sturtevant

WPF边界期望高度

  •  10
  • Joseph Sturtevant  · 技术社区  · 17 年前

    以下 Microsoft example code 包含以下内容:

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

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

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

    尽管存在此错误,代码仍能正常工作。我已经查阅了文档和 DesiredHeight Border . 谁能解释一下在哪里 期望高度

    7 回复  |  直到 17 年前
        1
  •  9
  •   Carlo    17 年前

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

    编辑:

    Border content = new Border();
    int desiredHeight = content.DesiredSize.Height;
    int desiredWidth = content.DesiredSize.Width;
    

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

        2
  •  6
  •   slm Vikas Hardia    13 年前

    有同样的问题。正在使用自定义 Expander ComboBox . 以上这些对我都不起作用,对我来说都没有约束力 Height 破坏了 ,使用 StackPanel 还中断了每组项目的显示。我发现:

    <Setter TargetName="ContentRow" Property="Height" Value="Auto"/>
    
        3
  •  4
  •   SonicBison    15 年前

    我碰到过这个。按照user275587的说法,他们的示例之所以有效,是因为触发器删除了行定义上的Heigth=“0”。

    因此,我切换了高度设置/触发逻辑,因此RowDefinition没有设置高度

    <Grid.RowDefinitions>
         <RowDefinition Height="Auto"/>
         <RowDefinition Name="ContentRow" />
    </Grid.RowDefinitions>
    ...
    <ControlTemplate.Triggers>
         <Trigger Property="IsExpanded" Value="False">
                 <Setter TargetName="ContentRow" Property="Height" Value="0" />
         </Trigger>
    </ControlTemplate.Triggers>
    
        4
  •  3
  •   kichijitsu    14 年前

    试试看。

    <Setter
        TargetName="content"
        Property="Height"
        Value="{Binding ElementName=content, Path=DesiredHeight}"
    />
        ↓
    <Setter TargetName="content" Property="Height" Value="NaN"/>
    

    绑定是不必要的。

        5
  •  2
  •   Drew Noakes    16 年前

    我在申请书中遇到了同样的问题。最后,我修改了代码,以便在两种模式之间切换内容的可见性 Collapsed Visible Grid StackPanel .

    我通常发现MS控件模板示例的质量非常好,但是这一个的错误有点令人沮丧。

        6
  •  2
  •   jsk    14 年前

    如果您有一些内容需要在已经扩展的扩展器中进行扩展,则绑定到的内容将不会进行扩展 理想尺寸。高度 ,你需要 -可能是由于用户275587给出的原因。

        7
  •  1
  •   user275587    16 年前

    DesiredHeight来自content元素,它是一个有效的绑定。我认为您的绑定无法解决的原因是DesiredHeight依赖于Height属性,并且您尚未在模板中设置固定高度,因此它的计算结果为Double.Nan