我有一个控制这个网格:
<Grid SizeChanged="Grid_SizeChanged" MaxWidth="{x:Static local:PaneCtrl.MAX_NO_SCALE_WIDTH}">
<Viewbox MaxHeight="32" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,5">
<DockPanel Width="{x:Static local:PaneCtrl.MAX_NO_SCALE_WIDTH}">
<StackPanel x:Name="PlaybackButtons" Orientation="Horizontal" Height="32" HorizontalAlignment="Center" >
<StackPanel.Effect>
<DropShadowEffect ShadowDepth="1" />
</StackPanel.Effect>
<Button x:Name="CornerRewind" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlFastBackward" >
<Image Source="/Pics/FastBackBtn.png" />
</Button>
<Button x:Name="CornerStop" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlStop" >
<Image Source="/Pics/StopBtn.png" />
</Button>
<Button x:Name="CornerPlay" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlPlay" >
<Image Source="/Pics/PlayBtn.png"/>
</Button>
<Button x:Name="CornerPause" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlPause" >
<Image Source="/Pics/PauseBtn.png"/>
</Button>
<Button x:Name="CornerFastForward" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlFastForward" >
<Image Source="/Pics/FastFwdBtn.png"/>
</Button>
<Button x:Name="CornerStepBack" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlBackAfewSec" >
<Image Source="/Pics/BackAfewSecBtn.png"/>
</Button>
<Button x:Name="CornerGoToLive" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlLive" >
<Image Source="/Pics/LiveBtn.png"/>
</Button>
</StackPanel>
</DockPanel>
</Viewbox>
</Grid>
这个XAML的作用是当它放在一个宽度为&gt; = PaneCtrl.MAX_NO_SCALE_WIDTH的容器中时,它以原始大小显示按钮,但是当它放在宽度为&lt; PaneCtrl.MAX_NO_SCALE_WIDTH,它按下按钮。
我希望在代码中实现相同的效果。不要问我为什么 - 它需要很长时间才能解释。
所以我删除了ViewBox以及其它使其离开此XAML的元素:
<Grid SizeChanged="Grid_SizeChanged" >
<StackPanel x:Name="PlaybackButtons" Orientation="Horizontal" Height="32" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,5">
<StackPanel.Effect>
<DropShadowEffect ShadowDepth="1" />
</StackPanel.Effect>
<Button x:Name="CornerRewind" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlFastBackward" >
<Image Source="/Pics/FastBackBtn.png" />
</Button>
<Button x:Name="CornerStop" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlStop" >
<Image Source="/Pics/StopBtn.png" />
</Button>
<Button x:Name="CornerPlay" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlPlay" >
<Image Source="/Pics/PlayBtn.png"/>
</Button>
<Button x:Name="CornerPause" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlPause" >
<Image Source="/Pics/PauseBtn.png"/>
</Button>
<Button x:Name="CornerFastForward" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlFastForward" >
<Image Source="/Pics/FastFwdBtn.png"/>
</Button>
<Button x:Name="CornerStepBack" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlBackAfewSec" >
<Image Source="/Pics/BackAfewSecBtn.png"/>
</Button>
<Button x:Name="CornerGoToLive" Style="{StaticResource ImageButtonStyle}" PreviewMouseLeftButtonDown="CornerPlayerControlLive" >
<Image Source="/Pics/LiveBtn.png"/>
</Button>
</StackPanel>
</Grid>
然后我添加了resize事件处理程序:
private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (e.NewSize.Width < PaneCtrl.MAX_NO_SCALE_WIDTH)
{
double scale = e.NewSize.Width / PaneCtrl.MAX_NO_SCALE_WIDTH;
PlaybackButtons.RenderTransform = new ScaleTransform(scale, scale, PlaybackButtons.ActualWidth / 2, PlaybackButtons.ActualHeight);
}
else
{
PlaybackButtons.RenderTransform = null;
}
}
我看到的是它的行为方式类似,但当容器宽度太小以至于原始尺寸的某些按钮被切断(未显示)时,在我缩放StackPanel后它们也没有显示虽然现在应该有足够的空间来展示它们。
很自然,我的问题是为什么?
可能是因为WPF首先确定需要隐藏它们,然后引发SizeChanged事件,允许我缩放它们并且不会重新安排&#34;他们之后。
我应该挂钩一个不同的事件,还是应该调用一些东西来强制WPF重新安排&#34;或者什么?