C# IsEnabled属性不能绑定到DependencyProperty和IValueConverter
我有以下代码: XAML代码:C# IsEnabled属性不能绑定到DependencyProperty和IValueConverter,c#,wpf,data-binding,C#,Wpf,Data Binding,我有以下代码: XAML代码: <Window x:Class="combobinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expres
<Window x:Class="combobinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:combobinding"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
<local:EnumConverter x:Key="isEnabledConverter" />
</Window.Resources>
<Grid>
<TextBox Text="Hello" IsEnabled="{Binding SectionTitle, Converter={StaticResource isEnabledConverter}}" />
</Grid>
</Window>
C#代码
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static readonly DependencyProperty SectionTitleProperty =
DependencyProperty.Register(nameof(SectionTitle),
typeof(SectionTitle),
typeof(MainWindow));
public SectionTitle SectionTitle
{
get { return (SectionTitle)GetValue(SectionTitleProperty); }
set { SetValue(SectionTitleProperty, value); }
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SectionTitle = SectionTitle.TitleBlock;
}
}
public enum SectionTitle
{
Normal,
TitleBlock
}
public class EnumConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var sectionType = (SectionTitle)value;
if (sectionType == SectionTitle.Normal)
return true;
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
//
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
公共静态只读从属属性Section TitleProperty=
受抚养人财产登记簿(姓名(部门名称),
类型(章节标题),
类型(主窗口);
公共部分标题部分标题
{
获取{return(SectionTitle)GetValue(SectionTitleProperty);}
set{SetValue(SectionTitleProperty,value);}
}
已加载私有无效窗口(对象发送器、路由目标)
{
SectionTitle=SectionTitle.TitleBlock;
}
}
公共枚举部分标题
{
典型的
标题栏
}
公共类EnumConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
var sectionType=(SectionTitle)值;
如果(sectionType==SectionTitle.Normal)
返回true;
返回false;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
抛出新的NotSupportedException();
}
}
我希望在设置dependencProperty
SectionTitle
时调用EnumConverter
,方法内的任何断点都会被命中
然而,情况似乎并非如此;而且,IsEnabled
属性没有像我所希望的那样绑定到SectionTitle
此代码有什么问题?问题在于
DataContext
。绑定找不到其目标
您可以在窗口的声明中设置上下文。将其添加到XAML中的窗口
标记中:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
问题在于
DataContext
。绑定找不到其目标
您可以在窗口的声明中设置上下文。将其添加到XAML中的窗口
标记中:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
您需要设置主窗口的DataContext。您可以在构造函数中轻松执行此操作:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
...
您需要设置主窗口的DataContext。您可以在构造函数中轻松执行此操作:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
...
使用
Name=“MyWindow”
在窗口上定义Name
属性,然后在绑定中使用它,如下所示:
<TextBox Text="Hello" IsEnabled="{Binding ElementName=MyWindow, Path=SectionTitle, Converter={StaticResource isEnabledConverter}}" />
使用Name=“MyWindow”
在窗口上定义Name
属性,然后在绑定中使用它,如下所示:
<TextBox Text="Hello" IsEnabled="{Binding ElementName=MyWindow, Path=SectionTitle, Converter={StaticResource isEnabledConverter}}" />
我认为dependencProperty
已经处理inotifPropertyChanged
?我认为dependencProperty
已经处理inotifPropertyChanged
?