绑定到后面的代码中的width属性

use*_*623 6 wpf binding code-behind

我有一种情况,我需要用一个按钮创建视图框.对此的xaml如下:请观察viewbox的Width属性.宽度应根据滑动条增加/减少(向右移动增加它,向左移动减小它).如下所示我知道如何在xaml中执行它并且它工作正常.但我的要求是能够在后面的代码中创建视图框并为其分配属性.

 <WrapPanel x:Name="_wrpImageButtons" Grid.IsSharedSizeScope="True"
           ScrollViewer.CanContentScroll="True" d:LayoutOverrides="Height" 
           Margin="5">
    <Viewbox x:Name="_ScaleButton" 
             Width="{Binding Value, ElementName=ZoomSlider}" Stretch="Fill">
         <CustomButton:_uscVCARSImagesButton x:Name="_btnImage1"/>
    </Viewbox>
 </WrapPanel>
Run Code Online (Sandbox Code Playgroud)

谢谢.

Bot*_*000 7

这应该做你想要的:

Viewbox x = new Viewbox();
Binding bnd = new Binding("Value") { ElementName = "ZoomSlider"};
BindingOperations.SetBinding(x, Viewbox.WidthProperty, bnd);
// ... Code to insert the Viewbox into the WrapPanel etc.
Run Code Online (Sandbox Code Playgroud)