问题描述
|
我正在寻找一种方法来将一个用户控件中存在的单选按钮的IsChecked属性绑定到另一个用户控件中存在的按钮。两个用户控件都被加载到MainWindow中。任何想法都受到高度赞赏。
MainWindow的代码:
<Grid x:Name=\"LayoutRoot\">
<Frame Content=\"Frame\" Source=\"/WpfApplication1;component/Page1.xaml\"/>
<local:NavUserControl HorizontalAlignment=\"Center\" VerticalAlignment=\"Bottom\"/>
</Grid>
代码页1:
<Grid x:Name=\"LayoutRoot\">
<local:ContentUC HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\"/>
</Grid>
用户控制代码1:
<Grid x:Name=\"LayoutRoot\">
<RadioButton x:Name=\"RadioBt1\" Content=\"Enable Next Button\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\"/>
</Grid>
用户控件代码2:
<StackPanel x:Name=\"LayoutRoot\" Orientation=\"Horizontal\" VerticalAlignment=\"Bottom\" HorizontalAlignment=\"Center\" Margin=\"0,20\">
<Button Content=\"Back\" HorizontalAlignment=\"Left\" Width=\"75\"/>
<Button Content=\"Next\" HorizontalAlignment=\"Left\" Width=\"75\" IsEnabled=\"{Binding IsChecked,ElementName=RadioBt}\" />
</StackPanel>
解决方法
按照封装的原理,您需要ѭ5的父
UserControl
将该按钮的IsChecked
属性公开。然后,这使位于UserControl
外部的按钮绑定到该属性。
为此,您需要修改UserControl
的代码后面添加适当的属性(例如IsNextEnabled
)和DependencyProperty
(例如IsNextEnabledProperty
),然后映射到RadioButton
\的属性。