16

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

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

2 回答 2

25

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

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

签出:social.msdn.microsoft

于 2016-05-12T18:43:42.340 回答
24

和属性主要用于设置请求Width的尺寸。Height

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

<Binding RelativeSource="{RelativeSource ...}" Path="ActualWidth"/>
于 2012-05-31T12:33:16.523 回答