Wpf 强制ItemsControl面板重新绘制
好的,我有一个包含容器控件的列表框。每个容器控件都包含1-X小部件,这些小部件以完全定制的包装形式展示,工作非常出色。小部件的大小由两个因素决定/更新:Wpf 强制ItemsControl面板重新绘制,wpf,repaint,wrappanel,Wpf,Repaint,Wrappanel,好的,我有一个包含容器控件的列表框。每个容器控件都包含1-X小部件,这些小部件以完全定制的包装形式展示,工作非常出色。小部件的大小由两个因素决定/更新: 父列表框的大小(如果更改,则更新) 视图比例滑块(25%、50%、100%)-类似缩放按钮 如果更改了列表框的大小,则在我的代码中会相应地调整自定义面板重绘和小部件的大小。工作得很好 <Grid.RowDefinitions> <RowDefinition Height="*" />
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="5" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox Name="lstGroups" Grid.Column="0" ItemsSource="{Binding Path=TopLevelGroups}" BorderThickness="0" ScrollViewer.VerticalScrollBarVisibility="Hidden" SizeChanged="ListBox_SizeChanged" VerticalAlignment="Stretch"
SelectionMode="Extended"
Style="{ StaticResource ListBoxWithAutoScroll_Horizontal }"
>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
<StackPanel Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Center" Orientation="Horizontal">
<Label Margin="5">View Scale:</Label>
<Slider Name="sldView" Minimum="25" Maximum="100" IsSnapToTickEnabled="True" TickPlacement="BottomRight" Ticks="25,50,100" Width="125" ValueChanged="Slider_ValueChanged"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right" DockPanel.Dock="Right">
<Button Command="{Binding Path=StopCommand}" Content="Stop" Height="30" Width="60" Margin="1" IsEnabled="{Binding Path=CanStop}" x:Name="btnStop" />
<Button Command="{Binding Path=RunCommand}" Content="Run" Height="30" Width="60" IsEnabled="{Binding Path=CanRun}" x:Name="btnRun" HorizontalAlignment="Right" Margin="1"/>
</StackPanel>
……麻烦来了:
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
...code here
this.ResetViewScale(new Size(this.myListBox.ActualWidth, this.myListBox.ActualHeight));
//How can I repaint my list/panel?
}
private void ResetViewScale(Size e)
{
Size newSize = new Size(e.Height / 2, e.Height / 2);
switch (App.ApplicationViewScale)
{
case ViewScale.Full:
Size full = new Size(newSize.Height, newSize.Height);
App.TileSize = new Size(full.Height, full.Width);
break;
case ViewScale.Half:
Size half = new Size(newSize.Height / 2, newSize.Height / 2);
App.TileSize = new Size(half.Height, half.Width);
break;
case ViewScale.Quarter:
Size quarter = new Size(newSize.Height / 4, newSize.Height / 4);
App.TileSize = new Size(quarter.Height, quarter.Width);
break;
default:
Size defaultToFull = new Size(newSize.Height, newSize.Height);
App.TileSize = new Size(defaultToFull.Height, defaultToFull.Width);
break;
}
}
private void Slider\u值已更改(对象发送方,RoutedPropertyChangedEventArgs e)
{
…这里是代码
this.ResetViewScale(新大小(this.myListBox.ActualWidth,this.myListBox.ActualHeight));
//如何重新绘制列表/面板?
}
私有空间重置视图比例(尺寸e)
{
尺寸newSize=新尺寸(e.Height/2,e.Height/2);
开关(应用程序视图比例)
{
案例视图比例。完整:
全尺寸=新尺寸(newSize.Height,newSize.Height);
App.TileSize=新尺寸(全高、全宽);
打破
案例视图比例。一半:
大小一半=新大小(newSize.Height/2,newSize.Height/2);
App.TileSize=新尺寸(半高半宽);
打破
案例视图比例。季度:
尺寸季度=新尺寸(newSize.Height/4,newSize.Height/4);
App.TileSize=新尺寸(四分之一高、四分之一宽);
打破
违约:
大小defaultToFull=新大小(newSize.Height,newSize.Height);
App.TileSize=新尺寸(defaultToFull.Height,defaultToFull.Width);
打破
}
}
我尝试使用所有的无效方法,但似乎没有任何效果。Sorta卡住了…尝试无效测量,在无效测量之前,您可以在ResetViewScale中显示您正在执行的操作,感谢您的响应,但我所做的只是设置小部件的大小。需要注意的是,这段代码在上述两个事件中都会触发,但ListBox会在SizeChanged上重新绘制,当然不会在我的滑块值更改时重新绘制:我假设自定义小部件的大小绑定到App.TileSize?能否显示影响其大小的CustomWidget上的DependencyProperty(或属性)?每个widget的高度和宽度由每个widget类型中的App.TileSize确定ViewModel:public double WidgetXHeight{get{return App.TileSize.Height;}公共双WidgetXWidth{get{return App.TileSize.Width*2;}
private void ListBox_SizeChanged(object sender, SizeChangedEventArgs e)
{
//Works perfect
this.ResetViewScale(e.NewSize);
}
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
...code here
this.ResetViewScale(new Size(this.myListBox.ActualWidth, this.myListBox.ActualHeight));
//How can I repaint my list/panel?
}
private void ResetViewScale(Size e)
{
Size newSize = new Size(e.Height / 2, e.Height / 2);
switch (App.ApplicationViewScale)
{
case ViewScale.Full:
Size full = new Size(newSize.Height, newSize.Height);
App.TileSize = new Size(full.Height, full.Width);
break;
case ViewScale.Half:
Size half = new Size(newSize.Height / 2, newSize.Height / 2);
App.TileSize = new Size(half.Height, half.Width);
break;
case ViewScale.Quarter:
Size quarter = new Size(newSize.Height / 4, newSize.Height / 4);
App.TileSize = new Size(quarter.Height, quarter.Width);
break;
default:
Size defaultToFull = new Size(newSize.Height, newSize.Height);
App.TileSize = new Size(defaultToFull.Height, defaultToFull.Width);
break;
}
}