我是 WPF 的新手,所以这是一个非常愚蠢的问题。
在 Visual Studio -> 属性中,当我选择一个 StackPanel(例如)时,我有属性宽度。在此属性中,我可以单击右侧的小方 block 并打开一个菜单。菜单中的选项之一是“自定义表达式”
所以这是我的问题:
是否可以在数学表达式中定义宽度和高度基础?
<StackPanel Width="{Parent.Width - 100}">
</StackPanel>
或者类似的东西?
编辑
我问这个是因为我打算创建一个 StackPanel,它的宽度需要比窗口低 100 像素。当窗口大小发生变化时,StackPanel 需要根据此规则进行更改。
最佳答案
默认情况下你不能,你必须使用转换器。当然,尤其是因为您是 WPF 的新手,为这样的每个简单操作一遍又一遍地编写转换器可能会让您感到痛苦。因此,有一些自定义标记扩展可以减轻这种痛苦。例如:https://quickconverter.codeplex.com/ (但还有其他)。他们看起来像这样:
<Window x:Class="WpfApplication2.MainWindow"
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"
xmlns:qc="http://QuickConverter.CodePlex.com/"
mc:Ignorable="d"
Title="MainWindow" Height="500" Width="500" x:Name="self">
<Grid x:Name="LayoutRoot">
<Rectangle Fill="Red"
Width="{qc:Binding '$P-100', P={Binding ElementName=self, Path=ActualWidth}}"
Height="{qc:Binding '$P-100', P={Binding ElementName=self, Path=ActualHeight}}" />
</Grid>
</Window>
这里我们将矩形的宽度和高度绑定(bind)到父“自身”元素尺寸减去 100,而不使用显式转换器。
关于c# - 在 WPF 中定义宽度和高度的自定义表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36458766/