My WrapPanel放置在网格*大小的单元格内。现在我有一个置于此WrapPanel内部的视图框。我想将WrapPanel的高度除以3,然后将其绑定到ViewBox。所以我试过使用转换器。但我仍然没有对ViewBox的高度或宽度进行任何更改。
这是我的代码:
<WrapPanel x:Name="wpTilesContainer">
<Viewbox x:Name="viewbox1"
Width="{Binding Width, Converter={StaticResource TilesHeightAndWidthConverter}, ElementName=wpTilesContainer}"
Height="{Binding Height, Converter={StaticResource TilesHeightAndWidthConverter}, ElementName=wpTilesContainer}">
<Grid>
<Rectangle Fill="DodgerBlue" Stroke="White" StrokeThickness="3" Height="{Binding Height, ElementName=viewbox1}" Width="{Binding Width, ElementName=viewbox1}" />
<TextBlock Text="HAEMOGRAM REPORT" FontSize="32" FontWeight="Bold" Margin="5" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Viewbox>
</WrapPanel>
以下是Converter的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace Lab_Lite.Converter
{
class TilesHeightAndWidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double result;
result = (double)value / (double)3;
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
答案 0 :(得分:1)
将其绑定到ActualHeight
/ ActualWidth
而不是Height
/ Width
。这是代码:
<WrapPanel x:Name="wpTilesContainer">
<Viewbox x:Name="viewbox1"
Width="{Binding ActualWidth, Converter={StaticResource TilesHeightAndWidthConverter}, ElementName=wpTilesContainer}"
Height="{Binding ActualHeight, Converter={StaticResource TilesHeightAndWidthConverter}, ElementName=wpTilesContainer}">
<Grid>
<Rectangle Fill="DodgerBlue" Stroke="White" StrokeThickness="3" Height="{Binding Height, ElementName=viewbox1}" Width="{Binding Width, ElementName=viewbox1}" />
<TextBlock Text="HAEMOGRAM REPORT" FontSize="32" FontWeight="Bold" Margin="5" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Viewbox>