IsEnabled属性不能绑定到DependencyProperty和IValueConverter

时间:2019-03-01 09:45:09

标签: 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/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();
        }
    }

我希望在设置EnumConverter DependencyProperty时会调用SectionTitle,并且方法中的任何断点都会被命中。

但是似乎并非如此;并且IsEnabled属性未按我的意愿绑定到SectionTitle

此代码有什么问题?

3 个答案:

答案 0 :(得分:3)

问题是DataContext。绑定找不到目标。

您可以在窗口的声明中设置上下文。将此添加到XAML中的Window标记中:

 DataContext="{Binding RelativeSource={RelativeSource Self}}"

答案 1 :(得分:1)

NameWindow上定义Name="MyWindow"属性,然后像下面这样在绑定中使用它:

<TextBox Text="Hello" IsEnabled="{Binding ElementName=MyWindow, Path=SectionTitle, Converter={StaticResource isEnabledConverter}}" />

答案 2 :(得分:0)

您需要设置MainWindow的DataContext。您可以在构造函数内进行此操作:

$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$sender_email."" . "\r\n"; 
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message_body));
$sentMail = mail($recipient_email, $subject, $body, $headers);