简体   繁体   中英

WPF bind control size

I have a StackPanel with a list of custom user controlls that I would like to resize. I would like the user to be able to drag a slider and scale the control size up and down.

Is there a way to bind the control width to a slider value? Something similar to:

<MyControl Width="{Binding Path=SizeSlider.SelectedValue}"/>

Is this possible? Or should I just iterate through the controls and manually set the size whenever the slider value changes?

You should be able to do this just fine by using

<MyControl Width="{Binding ElementName=SizeSlider, Path=Value}"/>

By only setting Path you assume that something named SizeSlider would exist in the current DataContext.

I've done this in code once and it worked:

var binding = new Binding("Value") { Source = slider };
BindingOperations.SetBinding(b, WidthProperty, binding);
BindingOperations.SetBinding(b, HeightProperty, binding);

So apparently

<MyControl Width="{Binding Source=SizeSlider, Path=Value}"/>

might be another way to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM