2011-05-16 49 views
5

我有一種情況,我需要用一個按鈕創建視圖框。這個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> 

謝謝。

回答

5

這應該做你想要什麼:

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. 
+0

我相信這是'FrameworkElement.WidthProperty'。 – user7116 2011-05-16 20:36:10

+0

哇,我從來沒有見過任何人使用'BindingOperations.SetBinding'。 – 2011-05-16 20:36:53

+0

我相信它沒有任何區別,因爲無論如何它都會解決該屬性。 – Botz3000 2011-05-16 20:37:32

4

您可以在代碼中創建後面比較容易的結合:

var widthBinding = new Binding("Value") { ElementName = "ZoomSlider" }; 

_ScaleButton.SetBinding(FrameworkElement.WidthProperty, widthBinding); 
相關問題