-1

I try to use a custom control with a DependencyProperty binding for the columns in a DataGrid. Everything is working up to the point where I select a row.

My custom control ShiftControl.xaml:

<UserControl x:Class="xRoster.UserControls.ShiftControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:xRoster.UserControls">
  <TextBlock x:Name="tbShift"
             Text="{Binding Path=Shift}"
             TextWrapping="Wrap"
             VerticalAlignment="Center"
             HorizontalAlignment="Center"/>
</UserControl>

My code-behind for the ShiftControl.xaml.cs:

public partial class ShiftControl : UserControl
{
    public ShiftControl()
    {
        InitializeComponent();
        tbShift.DataContext = this;
    }

    public static readonly DependencyProperty ShiftProperty =
        DependencyProperty.Register(
            "Shift", typeof(string), typeof(ShiftControl));

    public string Shift
    {
        get { return (string)GetValue(ShiftProperty); }
        set { SetValue(ShiftProperty, value); }
    }
}

The code where I use the ShiftControl in a DataGrid window.xaml:

<UserControl.Resources>
  <DataTemplate x:Key="day1Column">
    <uc:ShiftControl Shift="{Binding Day1Shift.Display}"/>
  </DataTemplate>
</UserControl.Resources>   

<DataGrid ItemsSource="{Binding Employees}"
          AutoGenerateColumns="False"
          HeadersVisibility="Column"
          RowHeight="50">
  <DataGrid.Columns>                
    <DataGridTemplateColumn Header="Montag"
                            HeaderStyle="{StaticResource columnHeaderStyle}" 
                            CellTemplate="{StaticResource day1Column}"
                            CellEditingTemplate="{StaticResource day1Column}"/>
  </DataGrid.Columns>
</DataGrid>

As I said, the Binding for my ShiftControl is working only when the Row where it is placed is not selected.

Any Ideas? Thanks in advance

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
J. Doe
  • 1
  • 2
  • This seems to be an issue only with TextBlocks. There is no problem with Labels, Buttons or Textboxes using the same Binding.. – J. Doe Mar 28 '17 at 20:45
  • See http://stackoverflow.com/questions/9035878/implicit-styles-in-application-resources-vs-window-resources. It's impossible to know for sure without a good [mcve], but based on your comment, it seems likely your question is a duplicate of that one. – Peter Duniho Mar 29 '17 at 00:06

1 Answers1

0

In your ShiftControl the DataBinding you set is not enough. Giving only Path for the Binding tries to bind the value to the specified Path of the current DataContext (which might change depending on your use of the control)

You should explicitly define the BindingSource in your ShiftControl.xaml

Try this:

<TextBlock x:Name="tbShift"
           Text="{Binding Path=Shift,
               RelativeSource={RelativeSource
                   Mode=FindAncestor,
                   Type={x:Type ShiftControl}}}"
           TextWrapping="Wrap"
           VerticalAlignment="Center"
           HorizontalAlignment="Center"/>
Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
Hasan
  • 2,444
  • 3
  • 30
  • 44
  • Isn't setting my DataContext in the Code-Behind enough ? in ShiftControl.xaml.cs `public ShiftControl() { InitializeComponent(); tbShift.DataContext = this; }` – J. Doe Mar 28 '17 at 20:42