2012-07-24 73 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

相关问题