WPF - 采用父元素的大小

18

我试图找出控件大小的最佳方法,但似乎做不对。我有一个窗口,在其中添加了自定义控件:

<Grid x:Name="LayoutRoot">
    <my:RateGraph Grid.Column="0" x:Name="rateGraph1" Height="88" Width="380" />
</Grid>

我希望调整XAML定义的控件的子组件大小,使其填充高度、宽度或两者都填充。但是我发现如果我去掉显式的宽度/高度,试图使用像VerticalAlignment="Stretch"这样的东西,那么我得到的控件大小为0...我做错了什么?

<rb:RateBase x:Class="RateBar.RateGraph"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:rb="clr-namespace:RateBar"
             xmlns:sd="clr-namespace:System.Windows.Data"
             mc:Ignorable="d">
    <RangeBase.Resources>
        <rb:JScriptConverter x:Key="JScript" TrapExceptions="False"/>
        <ControlTemplate x:Key="rateGraphTemplate" TargetType="{x:Type rb:RateBase}">
            <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <rb:Axis Width="320" Height="88"/>
                <Rectangle Height="88" Fill="#9690EE90" x:Name="progress">
                    <Rectangle.Width>
                        <MultiBinding Converter="{StaticResource JScript}" ConverterParameter="values[0]/values[1]*values[2]">
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Value"/>
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Maximum"/>
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Width"/>
                        </MultiBinding>
                    </Rectangle.Width>
                </Rectangle>
                <Polygon Fill="#FF06B025" x:Name="graph" />
                <Label Canvas.Left="0" Width="380" HorizontalContentAlignment="Right" Foreground="Black" Content="{Binding Path=Caption, RelativeSource={RelativeSource TemplatedParent}}">
                    <Canvas.Bottom>
                        <MultiBinding Converter="{StaticResource JScript}" ConverterParameter="(values[2]*0.8)/values[1]*values[0]">
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Rate"/>
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="RateMaximum"/>
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Height"/>
                        </MultiBinding>
                    </Canvas.Bottom>
                </Label>
                <Line X1="0" X2="380" Stroke="Black">
                    <Canvas.Bottom>
                        <MultiBinding Converter="{StaticResource JScript}" ConverterParameter="(values[2]*0.8)/values[1]*values[0]">
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Rate"/>
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="RateMaximum"/>
                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Height"/>
                        </MultiBinding>
                    </Canvas.Bottom>
                </Line>
            </Canvas>
        </ControlTemplate>
    </RangeBase.Resources>
</rb:RateBase>
2个回答

28

我曾试图做类似的事情,发现以下方法效果很好:

    Width="{Binding RelativeSource={RelativeSource FindAncestor, 
    AncestorType={x:Type ListBox}},Path=ActualWidth}"

查看:social.msdn.microsoft


这是关于 ListBox 的特定编程内容,仅提供翻译文本。 - luka

26

WidthHeight属性主要用于设置请求的尺寸。

如果您正在使用具有拉伸等动态布局,请绑定到ActualWidthActualHeight属性:

<Binding RelativeSource="{RelativeSource ...}" Path="ActualWidth"/>

1
谢谢Ross,我实际上使用了RelativeSource得到了非常接近的东西,尽管我使用了“FindAncestor”和“Width”,但我会采纳你的建议。 - Ian
1
谢谢。我是这样做的,它很好用:Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=Width}" - Thai Anh Duc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接