读书人

关于旋钮的高度

发布时间: 2013-04-22 16:01:35 作者: rapoo

关于按钮的高度
需求是:假设有个按钮[button1],现在通过属性直接设置它的高度,然后,宽度会通过一定的算法(这里简单的假设为1.5倍)。

我的思路是:
通过Height绑定,把自身的Width和self传进去然后通过自定义的Converter把希望的高度值穿出来。但是这样貌似没有效果。

代码如下:

<Button Grid.Column="1" Width="222" Height="{Binding Path=Width, RelativeSource={RelativeSource Mode=Self}, Converter=Div2PartConverter, ConverterParameter={Binding RelativeSource={RelativeSource Mode=Self}}}">Test</Button>

但是我测试下,没有效果,为什么?

但宽度根据高度改变还是可以的。


//转换器
public class HeightToWidth : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double height = (double)value;
return height * 1.5;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}



<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="SilverlightApplication1.MainPage"
xmlns:local="clr-namespace:SilverlightApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<UserControl.Resources>
<local:HeightToWidth x:Key="HeightToWidth"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White">
<Button x:Name="btn1" Content="Button" HorizontalAlignment="Left" Margin="23,22,0,0" VerticalAlignment="Top" Height="75" Width="{Binding Height, Converter={StaticResource HeightToWidth}, ElementName=btn1}"/>
</Grid>
</UserControl>

读书人网 >CAD教程

热点推荐