0
votes

I have a ListBox, In it I have a custom DataTemplate set for the ListBox.ItemTemplate so the ListBox Items are Radio buttons styled as rounded buttons.

Is there any way I could bind another separate button on my View to the Height of the ListBox.ItemTemplate's RadioButton's ActualHeight, so the separate button's height would always be the same height as the Radiobuttons in the ListBox?

1

1 Answers

0
votes

Here's a solution with some code behind. Handle the SizeChanged event for the RadioButton, and assign the given height to the view model's Height property. Bind the separate button's Height property to the Height property in the view model. Here's the XAML:

<ListBox ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <RadioButton
                Content="{Binding Name}"
                SizeChanged="OnRadioButtonSizeChanged" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
<Button
    Content="Button"
    Height="{Binding Height}"
    />

And here's the code behind:

private void OnRadioButtonSizeChanged(object sender, SizeChangedEventArgs e)
{
    ViewModel.Height = e.NewSize.Height;
}

Of course, the view model's Height property should raise the PropertyChanged event.