James Davidson
James Davidson

Reputation: 1

How to get value of ActualHeight, pass it into a converter which feeds the result into ActualWidth

newbie to xaml here. I have a question for you all.

I have a UserControl called "ResourceMonitor" and I am trying to make sure that when its surrounding task bar gets scaled (already written and tested), that the ActualWidth of my UserControl gets scaled 1 and a half times the ActualHeight (as to keep things consistent when scaling).

I am trying to use RelativeSource to retrieve the size of ActualHeight (of the ResourceMonitor UserControl), and then pass it as a parameter to my converter, and then assign it to width. Is there a better/easier way to do this?

<StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,2,0">
    <rm:ResourceMonitor Margin="0,0,4,0" 
                    ShowMemoryTrace="True" 
                    MemoryTraceColor="#FF3399FF"
                    Width="{Binding Path = {Path=ActualHeight, RelativeSource={RelativeSource Self}}, Converter={StaticResource ResourceMonitorWidthScalingConverter}}">
    </rm:ResourceMonitor>
    <Viewbox>
        <StackPanel  Orientation="Horizontal">
            <local:ServerStatus DataContext="{Binding ServerStatusViewModel}"/>
        </StackPanel>
    </Viewbox>
</StackPanel>

I am getting a couple errors here, one is saying "Type path is used like a markup extension but does not derive from MarkupExtension"

and

"The arguement name is missing"

Please help :/

Upvotes: 0

Views: 832

Answers (1)

Jai
Jai

Reputation: 8363

Your binding expression is written wrongly.

Width="{Binding Path=ActualHeight, RelativeSource={RelativeSource Self}},
                Converter={StaticResource ResourceMonitorWidthScalingConverter}}"

Using converter is one of the recommended way to do it for MVVM. However, if you don't mind code-behind, you can do it in code-behind by handling events. But seriously, using converter is much easier.

Upvotes: 1

Related Questions