0

So here's the situation: I have a label (custom control derviative of it, if it matters), and I need to get its width and height with MVVM. However, if I set either of the parameters to {Binding XXX}, they are no long Auto and thus when I change the font in runtime their size doesn't update.

I read about using ActualWidth/Height, which sounds like just what I need besides the fact that it's not a dependence parameter, thus it seemss like I'd need to break MVVM for it.

Are there any better solutions?

EDIT

Well, the element in XAML looks nothing special. Just a million of bindings.

<local:DraggableLabel 
                    Content="123" 
                    Margin="{Binding Label2Position, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                    HorizontalAlignment="Left" 
                    VerticalAlignment="Top" 
                    FontFamily="{Binding Label2Font.Family}" 
                    FontSize="{Binding Label2Font.Size}" 
                    FontWeight="{Binding Label2Font.Weight}" 
                    FontStyle="{Binding Label2Font.Style}" 
                    Foreground="{Binding Path=Label2Color, 
                                    UpdateSourceTrigger=PropertyChanged, 
                                    Converter={StaticResource ColorToBrushConverter}
                           }"/>

The default is,

Width="Auto"

which doesn't have to be written explicits, but can (changes nothing). It makes it resize when the font changes. If I set it to

Width="Binding {Label1Width}"

The binding works fine, but I no longer get the auto-adjustment.

12
  • Can you show your code please? Commented Mar 15, 2020 at 22:16
  • Added. No revelations, though. I can post the custom control code as well, but it doesn't mess with this at all, and since it derives from Label should behave the same. Commented Mar 15, 2020 at 22:37
  • Is the Label width also set to Auto? Commented Mar 15, 2020 at 22:37
  • Both Width and Height are set to Auto via the properties panel, it's not expressed in XAML but I tested and setting them to Auto in XAML does the same thing. Commented Mar 15, 2020 at 22:39
  • Since the ActualWidhth property is the binding source it doesn't have to be a DependencyProperty. You should use this. Also is Label1Width a custom DependencyProperty of type double? Commented Mar 15, 2020 at 22:40

1 Answer 1

1

Okay, so I found a workaround.

Since I had create a custom control, I was able to create a new DependencyProperty called RealWidth, then I added a OnSizeChanged event that updates it with the value of ActualWidth that you CAN get from inside the element every time the size of the element changes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.