0
votes

I am trying to find answer to my problem without success. didn't found anyone trying to do something similar.

I am simply trying to have a default scale factor on a DataGrid (object doesn't matter) and have it manually editable. Now let me explain in more detail.

I have a Slider with value range from 10 to 100 with Interval ticks of 10.

<Slider Name="sldZoom" 
        IsSnapToTickEnabled="True" 
        Height="180" 
        Orientation="Vertical" 
        TickPlacement="Both" 
        Minimum="10" 
        Maximum="100" 
        Ticks="10, 20, 30, 40, 50, 60, 70, 80, 90, 100" 
        Value="10" HorizontalAlignment="Center" />

Then i have a ScrollViewer with fix Height and Width for format purpose

<ScrollViewer Name="scrZoomPanel" 
              HorizontalScrollBarVisibility="Auto" 
              VerticalScrollBarVisibility="Auto" 
              Height="500
              Width="750" 
              Background="White">

And at last I have in the scrollviewer a DataGrid with random columns and row quantity

<DataGrid Name="grdArrangement"
          HorizontalAlignment="Stretch" 
          HorizontalContentAlignment="Stretch" 
          VerticalAlignment="Top" 
          ItemsSource="{Binding Path=ListData}" 
          IsHitTestVisible="False" 
          CanUserAddRows="False" 
          CanUserDeleteRows="False"
          CanUserReorderColumns="False" 
          CanUserSortColumns="False" 
          HeadersVisibility="None" 
          AutoGenerateColumns="True" 
          GridLinesVisibility="None" 
          SelectionMode="Single"       
          AutoGeneratingColumn="grdArrangement_AutoGeneratingColumn"  
          AutoGeneratedColumns="grdArrangement_AutoGeneratedColumns" 
          Background="White" >
          <DataGrid.RenderTransform>
              <ScaleTransform  
                              ScaleX="{Binding ElementName=sldZoom, Path=Value, Converter={StaticResource ZoomRatio}}" 
                              ScaleY="{Binding ElementName=sldZoom, Path=Value, Converter={StaticResource ZoomRatio}}" />
          </DataGrid.RenderTransform>
  </DataGrid>

the 2 auto generate event is because the grid get different cell styles assigned to it eveytime datasource changes

So datagrid can get different amount of rows and columns and at Zoom Value 100 the scale is 1. At Zoom value of 10 scale is 0.1.

In some case the grid get's bigger than the scrollviewer and it shows scrollbar and that's fine but what i want is : after the binding is complete and onpaint completed (everything generated), I want to zoom to the level that allow me to see everything. If not possible because the datagrid is too big i need to stop at Zoom 10 (10%). I need the zoom factor to stop ONLY on ticks that is inside the list.

I tried iterating over the slide ticks and set the zoom and check if the scrollviewer still display scrollbars but that doesn't work the scrollviewer always show value before everything change. By that i mean if the grid was 1x1 pixel the scroll viewer scroll ba visible was false even if it zommed in enought for the 1x1 grid to be 1000 % zoom so 1000x1000 pixel and scrollbar actually shows. onve code finish running i click a button that message box scrollbar visibility and i see them as true.

Seems like changing the slider value doesn't update the scale transform of the grid right away.

So in short i simply want a Scale To Fit and that happen only once every binding update of the datagrid. I also know exactly everytime the grid datasource changes.

thank you

Edited Datagrid. Code showed LayoutTransform and not RenderTransform. I copied while i was testing LayoutTransform instead just to see if that was it. Anyhow both work on Zoom but zoom to fit fails

1
If I'm understanding correctly, I think you need to use the Measure method to manually determine the best scale factor. Measure will set the DesiredSize, which you can use to determine how much scaling is needed to get a scale-to-fit by comparing it to the container's size. Also, for this to work, you may need to use LayoutTransform. Here is info about the Measure method. msdn.microsoft.com/en-us/library/…Jason Tyler
Well i am going to go with LayoutTransform that's for sure now. RenderTransform does not affect the DataGrid border but LayoutTransform does. That link does work in this case as the grid always have undetermined Width unless it surpasses the container. But still that is perfect for 2 other controls i have that are very similar to this problematic control and scaling will be for images. Thank youFranck
Ah, I didn't know that about the DataGrid. That's a bummer. Glad it worked out in the other cases for ya, though.Jason Tyler

1 Answers

0
votes

I started looking at the problem form another angle and try getting

scrZoomPanel.ComputedHorizontalScrollBarVisibility

and

scrZoomPanel.ComputedHorizontalScrollBarVisibility

to be anything else than original value before altering the DataGrid.

So i was looking to a way to force refresh of the scrZoomPanel and found ou there is a function for that :

scrZoomPanel.UpdateLayout();

If i call that every time just before trying to read the scrZoomPanel.ComputedHorizontalScrollBarVisibility then it finally return me the real value.

But i find that a bit invasive needing to call that every single time. Wonder if there is a way to make a binding change force refresh in XAML instead of background code.