为什么 UserControl 不使用绑定和 x:Name?
why UserControl don't use binding and x:Name?
我想使用 UserControl,例如 ContentControl。
例如,如果 CheckButton Check,则更改 Button 的 isEnalbe 属性。
但是,不要执行。
为什么??????
显示附加代码!
== Window.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();
}
}
您的 Button 嵌套在自定义控件中,无法直接访问 CheckBox。将 属性 添加到 CheckBox 绑定其 IsChecked 属性
<CheckBox x:Name="CheckBox" IsChecked="{Binding IsChecked}"/>
然后可以如下引用CheckBox的DataContext
<Button IsEnabled="{Binding RelativeSource={RelativeSource AncestorType=Grid} Path=DataContext.IsChecked}" Height="50"/>
我想使用 UserControl,例如 ContentControl。
例如,如果 CheckButton Check,则更改 Button 的 isEnalbe 属性。
但是,不要执行。
为什么??????
显示附加代码!
== Window.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();
}
}
您的 Button 嵌套在自定义控件中,无法直接访问 CheckBox。将 属性 添加到 CheckBox 绑定其 IsChecked 属性
<CheckBox x:Name="CheckBox" IsChecked="{Binding IsChecked}"/>
然后可以如下引用CheckBox的DataContext
<Button IsEnabled="{Binding RelativeSource={RelativeSource AncestorType=Grid} Path=DataContext.IsChecked}" Height="50"/>