1

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. 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. Do I do something very wrong? Here is my 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);}}

    }
}
Sacha
  • 134
  • 2
  • 12

1 Answers1

4

Doesn't look like you're setting your DataContext

You have to tell your xaml where to look for its data. 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. We do this so that when you start learning about MVVM you can make your DataContext a viewmodel and stop using the behind code.

Here is a simple example:

In your MainWindow.xaml

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

In your 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. I am doing this intentionally. Your xaml will only go and look for its property value once.

If you want it to update when you change the property then you need to implement INotifiyPropertyChanged

Which you can read about on the MSDN Article and on this Stack Overflow Article Implementing INotifyPropertyChanged - does a better way exist?

Community
  • 1
  • 1
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • Thank you Anthony for your answer. Setting DataContext in the constructor changes eventually lots of things. It works fine for my menu items binding but it always doesn't work for the bindings I have in my two TextBlock (bindings on Text and Background). Also for property update, I thought it works nicely without having to do something else. It seems me lot of complication for not a big deal. Honestly for a person who uses to develop in C++, it kind of seems to me easier to change everything manually instead of using these databindings. – Sacha Jun 28 '16 at 13:41
  • 1
    Ok I found out for TextBlocks, the problem was the type of my Color. xaml expects SolidColorBrush. – Sacha Jun 28 '16 at 13:56
  • @pashashadkami yes you must make sure you're passing the correct types into what you're trying to bind. You can also use converters. That said, every engineer ever ever who has started learning data bindings has said that it's easier to do it by hand. Until you actually learn how to do it and how powerful it is with MVVM. Then you never look back. – DotNetRussell Jun 28 '16 at 14:02