C# 为什么用户控件不';t使用绑定和x:Name?
我想使用UserControl,比如ContentControl 例如,如果CheckButton选中,则更改按钮的IsEnable属性 但是,不要执行 为什么 显示附加代码 ==Window.xaml==C# 为什么用户控件不';t使用绑定和x:Name?,c#,wpf,binding,C#,Wpf,Binding,我想使用UserControl,比如ContentControl 例如,如果CheckButton选中,则更改按钮的IsEnable属性 但是,不要执行 为什么 显示附加代码 ==Window.xaml== <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4" x:Class="WpfApplication4.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:UserControl1 >
<local:UserControl1.Buttons>
<Button IsEnabled="{Binding Path=IsChecked, ElementName=CheckBox}" Height="50"/>
<Button Height="50"/>
</local:UserControl1.Buttons>
</local:UserControl1>
<CheckBox x:Name="CheckBox"/>
</Grid>
</Window>
==UserControl.xaml==
<UserControl x:Class="WpfApplication4.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel x:Name="MainStack">
</StackPanel>
</UserControl>
==UserControl.xaml.cs==
public partial class UserControl1 : UserControl
{
public ObservableCollection<Button> Buttons
{
get { return (ObservableCollection<Button>)GetValue(ButtonsProperty); }
set { SetValue(ButtonsProperty, value); }
}
public static readonly DependencyProperty ButtonsProperty =
DependencyProperty.Register("Buttons", typeof(ObservableCollection<Button>), typeof(UserControl1), new PropertyMetadata(
(depObj, args) =>
{
var dep = depObj as UserControl1;
var newItem = args.NewValue as ObservableCollection<Button>;
newItem.CollectionChanged += dep.ButtonsOnCollectionChanged;
}));
private void ButtonsOnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args)
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var newItem in args.NewItems)
{
var input = newItem as Button;
MainStack.Children.Add(input);
}
break;
case NotifyCollectionChangedAction.Reset:
MainStack.Children.Clear();
break;
}
}
public UserControl1()
{
Buttons = new ObservableCollection<Button>();
InitializeComponent();
}
}
公共部分类UserControl1:UserControl
{
公共可观察收集按钮
{
获取{return(observateCollection)GetValue(buttonProperty);}
set{SetValue(按钮属性,值);}
}
公共静态只读从属属性按钮属性=
DependencyProperty.Register(“按钮”、typeof(ObservableCollection)、typeof(UserControl1)、new PropertyMetadata(
(depObj,args)=>
{
var dep=depObj作为UserControl1;
var newItem=args.NewValue作为ObservableCollection;
newItem.CollectionChanged+=dep.ButtonSCollectionChanged;
}));
私有void按钮OnCollectionChanged(对象发送方,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
{
开关(参数操作)
{
案例NotifyCollectionChangedAction。添加:
foreach(args.NewItems中的var newItem)
{
var input=newItem as按钮;
MainStack.Children.Add(输入);
}
打破
案例通知CollectionChangedAction.Reset:
MainStack.Children.Clear();
打破
}
}
公共用户控制1()
{
按钮=新的ObservableCollection();
初始化组件();
}
}
您的按钮嵌套在自定义控件中,无法直接访问复选框。将属性添加到WCH复选框将绑定其IsChecked属性
<CheckBox x:Name="CheckBox" IsChecked="{Binding IsChecked}"/>
然后,您可以参考复选框的DataContext,如下所示
<Button IsEnabled="{Binding RelativeSource={RelativeSource AncestorType=Grid} Path=DataContext.IsChecked}" Height="50"/>
您的按钮嵌套在自定义控件中,无法直接访问复选框。谢谢!那个么,若在窗口中使用复选框,怎么办?