Wpf 在xaml元素绑定语句中,除了使用转换器之外,还有其他方法可以进行简单的计算吗?
在XAML中,我希望将一个元素的高度绑定为另一个元素高度的一半。 有没有一种方法不需要在代码背后编写转换器 例如:- 我所拥有的Wpf 在xaml元素绑定语句中,除了使用转换器之外,还有其他方法可以进行简单的计算吗?,wpf,xaml,binding,converter,Wpf,Xaml,Binding,Converter,在XAML中,我希望将一个元素的高度绑定为另一个元素高度的一半。 有没有一种方法不需要在代码背后编写转换器 例如:- 我所拥有的 <Button Name="RemoveButton" Content="Remove Stage" Width="100" Height="{Binding ElementName=AddButton, Path=Height, Converter={StaticResource MyHalfHeightConverter}}"/> 我想要的是
<Button Name="RemoveButton" Content="Remove Stage" Width="100" Height="{Binding ElementName=AddButton, Path=Height, Converter={StaticResource MyHalfHeightConverter}}"/>
我想要的是
<Button Name="RemoveButton" Content="Remove Stage" Width="100" Height="{Binding ElementName=AddButton, Path=(Height / 2.0)}"/>
我认为没有没有转换器的绑定解决方案。但是为什么不用呢?您经常会遇到这样的需求,因此创建某种MathConverter是有意义的,它接受一些属性或参数。然后,您不需要为每个需求创建单独的转换器 但是,如果您确实不想使用转换器,根据您的布局,您也可以使用星形网格,其中AddButton分布在两行上,RemoveButton仅占一行:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Name="AddButton" Grid.Row="0" Grid.RowSpan="2" ... />
<Button Name="RemoveButton" Grid.Row="1" ... />
</Grid>
如果希望RemoveButton垂直居中,请使用以下命令:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Button Name="AddButton" Grid.Row="0" Grid.RowSpan="3" ... />
<Button Name="RemoveButton" Grid.Row="1" ... />
</Grid>
这样,AddButton占据三行(总共4*行),而RemoveButton占据中间一行(2*)
如果无法将它们添加到一个共享网格,则可以使用
Grid.IsSharedSizeScope
attached属性。可以找到详细信息。我认为没有没有转换器的绑定解决方案。但是为什么不用呢?您经常会遇到这样的需求,因此创建某种MathConverter是有意义的,它接受一些属性或参数。然后,您不需要为每个需求创建单独的转换器
但是,如果您确实不想使用转换器,根据您的布局,您也可以使用星形网格,其中AddButton分布在两行上,RemoveButton仅占一行:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Name="AddButton" Grid.Row="0" Grid.RowSpan="2" ... />
<Button Name="RemoveButton" Grid.Row="1" ... />
</Grid>
如果希望RemoveButton垂直居中,请使用以下命令:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Button Name="AddButton" Grid.Row="0" Grid.RowSpan="3" ... />
<Button Name="RemoveButton" Grid.Row="1" ... />
</Grid>
这样,AddButton占据三行(总共4*行),而RemoveButton占据中间一行(2*)
如果无法将它们添加到一个共享网格,则可以使用Grid.IsSharedSizeScope
attached属性。详情请参阅