WPF - 采用父级的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10832745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:25:52  来源:igfitidea点击:

WPF - Adopt size of parent

wpfsizeparent

提问by Ian

I'm trying to figure out the best way to size some controls but can't quite get it right. I have a window that adds on a custom control:

我试图找出调整某些控件大小的最佳方法,但不能完全正确。我有一个添加自定义控件的窗口:

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

I then wish to size the sub-components of this control defined in the XAML to fill either the height, width or both. What I find however is that if I take off the explicit width/height and try and use something like VerticalAlignment="Stretch" then I get a control of size 0... What am I doing wrong?

然后,我希望调整 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>

回答by Ross

The Widthand Heightproperties are primarily designed for setting requesteddimensions.

WidthHeight特性的设计主要是用于设定请求的尺寸。

If you are using dynamic layouts with stretching, etc. bind to the ActualWidthand ActualHeightproperties:

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

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

回答by Mike Casa

I was trying to do something similar, found this to work well:

我试图做类似的事情,发现这很好用:

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

Check out: social.msdn.microsoft

查看:social.msdn.microsoft