简体   繁体   English

C#WPF数据绑定不起作用

[英]C# WPF Databindings not working

I'm pretty new to c# and I'm trying to create a WPF window with menus and text-blocks but none of my data-binding work. 我对C#还是很陌生,我正在尝试创建一个带有菜单和文本块的WPF窗口,但我的数据绑定工作都没有。 I saw several pages and forums on the internet and I saw that people are always talking about setting a DataContext but I don't know why my MainWindow is not considered as DataContext. 我在互联网上看到了多个页面和论坛,并且看到人们一直在谈论设置DataContext,但是我不知道为什么我的MainWindow不被视为DataContext。 Do I do something very wrong? 我做错什么了吗? Here is my xaml: 这是我的xaml:

<Window x:Class="holdingseditor.MainWindow"
<Grid>
    <TextBlock Height="30" Margin="0,24,0,0" Width="675" Text="{Binding DbRibbonText}" Background="{Binding DbRibbonColor}"/>
    <TextBlock Height="30" Margin="675,24,0,0" Width="472" Background="{Binding WfRibbonColor}" Text="{Binding WfRibbonText}"/>
    <Menu HorizontalAlignment="Left" Height="24" Margin="0,0,0,0" VerticalAlignment="Top" Width="1155">
        <MenuItem Header="_View">
            <MenuItem Header="Show _Archived Files History" Height="22" FontSize="12" Margin="0" Click="M_ShowArchivedFiles" IsEnabled="{Binding Path=DiesenameLoaded}"/>
        </MenuItem>

        <MenuItem Header="_Workflow">
            <MenuItem Header="O_utside Mode" Height="22" FontSize="12" Margin="0" IsCheckable="true" IsChecked="{Binding IsWfOutside}"/>
        </MenuItem>
    </Menu>
</Grid>    
</Window>

And my properties look like that: 我的属性如下所示:

namespace holdingseditor
{
    public partial class MainWindow : Window
    {
        public bool DiesenameLoaded
        {get { return false; }}

        public bool IsWfOutside
        {get { return true; }}

        public string DbRibbonText
        {get {return "my text";}}

        public Color DbRibbonColor
        {get {return Color.FromArgb(255, 0, 0, 255);}}

    }
}

Doesn't look like you're setting your DataContext 看起来好像您没有设置DataContext

You have to tell your xaml where to look for its data. 您必须告诉xaml在哪里寻找其数据。 You probably see in your output window Binding Expression errors. 您可能会在输出窗口中看到“绑定表达式”错误。

In your constructor put 在构造函数中

this.DataContext = this;

This will tell your xaml to go to the MainWindow.cs file to look for the properties you're binding to. 这将告诉您的xaml转到MainWindow.cs文件以查找要绑定到的属性。 We do this so that when you start learning about MVVM you can make your DataContext a viewmodel and stop using the behind code. 我们这样做是为了在您开始了解MVVM时,可以将DataContext设为视图模型,并停止使用后面的代码。

Here is a simple example: 这是一个简单的示例:

In your MainWindow.xaml 在您的MainWindow.xaml中

<TextBlock Text="{Binding myTextProperty}"/>

In your MainWindow.xaml.cs 在您的MainWindow.xaml.cs中

public partial class MainWindow : Window{
      public String myTextProperty {get; set;}

      public MainWindow(){
          InitializeComponent();
          myTextPropety = "It works!";
          this.DataContext = this;
      }
}

Notice that I am setting the property before I set my DataContext. 请注意,在设置DataContext之前,先设置属性。 I am doing this intentionally. 我是故意这样做的。 Your xaml will only go and look for its property value once. 您的xaml只会查找其属性值一次。

If you want it to update when you change the property then you need to implement INotifiyPropertyChanged 如果要在更改属性时更新它,则需要实现INotifiyPropertyChanged

Which you can read about on the MSDN Article and on this Stack Overflow Article Implementing INotifyPropertyChanged - does a better way exist? 您可以在MSDN文章和此堆栈溢出文章上阅读有关实现INotifyPropertyChanged的内容-是否存在更好的方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM