强制ItemsControl面板重绘

时间:2012-08-06 19:26:49

标签: wpf repaint wrappanel

好吧,我有一个包含容器控件的ListBox。每个容器控件包含1-X小部件,这些小部件在完全自定义的WrapPanel中布局,并且工作得很好。小部件的大小由2个因素确定/更新:

  1. 父列表框的大小(如果更改,则更新)
  2. 视图比例SliderBar(25%,50%,100%) - 类似于缩放按钮
  3. 如果更改了列表框大小,则会在我的代码中相应地调整自定义面板重新绘制和小部件的大小。工作完美。

      <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>
    

    - 小工具 -

     <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
             <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <StackPanel.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Edit" Command="{Binding Path=EditCommand}" IsEnabled="{Binding IsMonitorStopped}"/>
                    <MenuItem Header="Remove" Command="{Binding Path=RemoveCommand}" IsEnabled="{Binding IsMonitorStopped}"/>
                </ContextMenu>
            </StackPanel.ContextMenu>
            <Image Source="" Margin="2" />
            <Label Content="{Binding Path=Name}" Margin="5,0,0,0" VerticalAlignment="Center"/>
        </StackPanel>
            <ItemsControl Grid.Row="1" ItemsSource="{Binding Path=Entities}" Name="icEntities" >
            <ItemsControl.Resources>
                <DataTemplate DataType="{x:Type vm:AppleViewModel}">
                    <ct:AppleTile Height="{Binding Path=AppleHeight}" Width="{Binding Path=AppleWidth}" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
                </DataTemplate>
                <DataTemplate DataType="{x:Type vm:OrangeViewModel}">
                    <ct:OrangeTile Height="{Binding Path=OrangeHeight}" Width="{Binding Path=OrangeWidth}"  Grid.ColumnSpan="2"/>
                </DataTemplate>
                <DataTemplate DataType="{x:Type vm:GrapeViewModel}">
                    <ct:GrapeTile Height="{Binding Path=GrapeHeight}" Width="{Binding Path=GrapeWidth}" />
                </DataTemplate>
            </ItemsControl.Resources>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <!-- Custom Wrap panel -->
                    <co:MonitorPanel x:Name="mpMain"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
        </Grid>
    

    如果用户移动滑块以进行放大/缩小 - 我需要能够使用新计算的窗口小部件强制重新渲染自定义面板。

    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;
            }
        }
    

    我尝试使用所有Invalidate方法,但似乎没有任何工作。 Sorta卡住......

0 个答案:

没有答案