2011-02-27 31 views
1

我有一个带有布尔属性的代码隐藏的silverlight页面。如何绑定到silverlight中的页面属性?

在xaml中,我有一个tabcontrol,其中一个tabitem的内容是busyindicator。

我想要将busyindicator的isbusy属性绑定到代码隐藏的布尔属性,但无论使用哪种绑定语句,我都无法解决它。

回答

2

在代码隐藏中,您需要确保已经设置了DataContext。所以,在你的页面的Load事件,把这个:

this.DataContext = this; 

如果你已经做到了这一点,那么你将需要发布更多细节。

0

我有这样的事情对于一个标签

public string Caption { get; set; } 

    void UserControl_Loaded(object sender, RoutedEventArgs e) 
    { 
     Caption = "Label"; 
    } 

和XAML

<Label Content="Label" Height="28" Name="label1" DataContext="{Binding Caption}" /> 

我敢肯定,你可以做同样的事情忙指示。

6

Slugster确实有一个整洁的解决方案,但我认为你正在寻找的是这个

<Page x:Name="MyPage> 
<TabControl> 
    <TabItem> 
     <BusyIndicator IsBusy="{Binding ElementName=MyPage, Path=MyBooleanPropertyNameInCodeBehind}" /> 
    </TabItem> 
</TabControl> 

+0

是的,这肯定会奏效。 +1。 – slugster 2011-02-28 20:03:12

3

Slugster和AntSlay,无论您的解决方案的工作。我发现这也适用:

<Page DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
    <TabControl> 
     <TabItem> 
      <BusyIndicator IsBusy="{Binding MyBooleanPropertyNameInCodeBehind}" /> 
     </TabItem> 
    </TabControl> 
</Page> 
相关问题