2012-07-24 70 views
-1
<ContentControl Width="130" 
       Height="130" 
       Canvas.Top="60" 
       Canvas.Left="50" 
       ***Selector.IsSelected="True"*** 
       Style="{StaticResource DesignerItemStyle}"> 

我想用後面的代碼來設置屬性Selector.IsSelectedContentControl。但我不知道該怎麼做。請幫助我,舉個例子。WPF C#設置觸發屬性通過後面的代碼

回答

3

如果你想在代碼中設置一個附加的依賴屬性你這樣做

 ContentControl x; 
     //To set the value 
     x.SetValue(Selector.IsSelectedProperty, true); 

     //To Clear the value 
     x.ClearValue(Selector.IsSelectedProperty); 

     //Set using the static function on Selector 
     Selector.SetIsSelected(x, true); 
+0

非常感謝,完美的工作... – nsnara 2012-07-24 06:49:52

+1

@nsnara如果它適合你..你介意接受它作爲答案 – 2012-07-25 02:40:56

2

用於訪問代碼隱藏您需要先提供一個名稱對照 -

<ContentControl 
    x:Name=""ContentControl1" 
    Width="130" 
    Height="130" 
    Canvas.Top="60" 
    Canvas.Left="50" 
    ***Selector.IsSelected="True"*** 
    Style="{StaticResource DesignerItemStyle}"> 

,然後您可以在代碼中訪問它並設置其他答案中提到的值 -

ContentControl1.SetValue(Selector.IsSelectedProperty, true); 

除了m該這將是一個好主意,看看在代碼中創建隱藏或模型(MVVM)屬性,並直接結合,爲您的控制這樣的 -

<ContentControl 
    Width="130" 
    Height="130" 
    Canvas.Top="60" 
    Canvas.Left="50" 
    Selector.IsSelected="{Binding IsSelectedBoolProperty, Mode=OneWay}" 
    Style="{StaticResource DesignerItemStyle}"> 

此技術將是如果你有非常有用在你的窗口中有很多控件,我建議你看看在應用程序中實現MVVM,以避免在代碼隱藏中做這些事情。

+0

非常感謝,它完美的作品 – nsnara 2012-07-24 06:50:09

相關問題