Sacha
Sacha

Reputation: 164

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. 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);}}

    }
}

Upvotes: 1

Views: 194

Answers (1)

DotNetRussell
DotNetRussell

Reputation: 9857

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?

Upvotes: 5

Related Questions