سؤال

I need to figure out how to change the UserControl's Width and Height at runtime. I am trying to do this during a mouseMove event. The Width and Height are set to Auto in XAML.

'mouseDown event
mouseVerticalPosition = e.GetPosition(Nothing).Y
mouseHorizontalPosition = e.GetPosition(Nothing).X

'mouseMove event
Dim deltaV As Double = e.GetPosition(Nothing).Y - mouseVerticalPosition
Dim deltaH As Double = e.GetPosition(Nothing).X - mouseHorizontalPosition

Me.Width = Me.Width + deltaH
UpdateLayout() 'does not work

This code is inside of the UserControl not the Page where the UserControl lives - if that helps.

Consider the container and it's properties - auto and stretch are used here:

<Grid x:Name="LayoutRoot" Background="Transparent" >
 <Canvas Name="cnv" Margin="18">
      <Rectangle Name="Rect" Margin="0" />
 </Canvas>
</Grid>
هل كانت مفيدة؟

المحلول

The actual numeric value of an Auto setting on Width or Height is Double.NaN. When you add anything to that initial NaN value the result will always still be NaN. You can either use ActualWidth/ActualHeight instead as the number to add to or try adding a Double.IsNaN check before you set the value and if true just use ActualWidth/ActualHeight.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top