alperc
alperc

Reputation: 383

ItemsControl databinding doesn't work?

I have this WPF structure:

<UserControl 
      xmlns:viewModel="clr-namespace:..ViewModel.ToneAudiogramLegend">
      ...
  <DataTemplate DataType="{x:Type viewModel:ToneAudiogramLegendTableViewModel}">
      ...

    <DataGrid  Grid.Row="1" Grid.Column="0" ItemsSource="{Binding ToneAudiogramLegneds}" HeadersVisibility="None" AutoGenerateColumns="False" IsReadOnly="True" BorderBrush="Transparent" BorderThickness="0"
                                   MinWidth="100" Height="{Binding Height, Mode=OneWay}" KeyboardNavigation.DirectionalNavigation="None" Grid.ColumnSpan="6" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" GridLinesVisibility="None" ColumnWidth="*"
                                  Margin="1" wpfmvvm:DataGridRowHeightBehaviour.AutoFitRowHeight="True" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
                            <DataGrid.Columns>
                                <DataGridTemplateColumn HeaderTemplate="{x:Null}">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Label x:Name="PART_Content" Content="{Binding Path=Left.Content}" 
                                            HorizontalAlignment="Center" VerticalAlignment="Center"
                                            Foreground="Gold"/>

                                            <DataTemplate.Triggers>
                                                  <DataTrigger Binding="{Binding Path=IsMonochrome}" Value="True">
                                                   <Setter TargetName="PART_Content" Property="Foreground" Value="Green"/>
                                                  </DataTrigger>
                                            </DataTemplate.Triggers>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
  ...
  <DataTemplate>

The binding of the Value works because the color gets the default value of Left.Color of <Setter Property="Foreground" Value="{Binding Path=Left.Color}"/>

The IsMonochrome property changes value when a specific event occurs, but the Foreground color does not change to green. I am not quite sure the format and the structure is appropriate. I am not sure if <DataTemplate> is a problem, since I have another <DataTemplate> in higher order

The classes:

public partial class ToneAudiogramLegendTableViewModel : ViewModelBase, IToneAudiogramLegendTableViewModel, IHandleMonochromeReportElement
{
    public bool IsMonochrome
            {
                get { return GetValue<bool>("IsMonochrome"); }
                private set { SetValue("IsMonochrome", value); }
            }

            public void SwitchToMonochromeMode()
            {
                IsMonochrome = true;
            }

            public void SwitchToColorMode()
            {
                IsMonochrome = false;
            }
}

and

public class ToneAudiogramLegendViewModel : ViewModelBase, IToneAudiogramLegendVM
    {
        public string Name
        {
            get { return GetValue<string>("Name"); }
            set { SetValue("Name", value); }
        }

        public LegendViewModel Left
        {
            get { return GetValue<LegendViewModel>("Left"); }
            set { SetValue("Left", value); }
        }
}

and

public class LegendViewModel : ViewModelBase
    {
        public object Content
        {
            get { return GetValue<object>("Content"); }
            set { SetValue("Content", value); }
        }

        public Brush Color
        {
            get { return GetValue<Brush>("Color"); }
            set { SetValue("Color", value); }
        }

        public LegendViewModel(object content, Brush color)
        {
            Content = content;
            Color = color;
        }
    }

What might be the issue?

Upvotes: 2

Views: 126

Answers (1)

Liero
Liero

Reputation: 27360

In your CellTemplate you are binding to ToneAudiogramLegendViewModel object, where are properties Name and Left. The IsMonochrome is on different object, that's why it does not work.

You need to either define IsMonochrome property or you need create property to reference ToneAudiogramLegendTableViewModel in ToneAudiogramLegendViewModel to be able to databind to IsMonochrome.

EDIT: based on your comments:

<DataTrigger Binding="{Binding Path=DataContext.IsMonochrome, 
                               RelativeSource={RelativeSource DataGrid}}" 
             Value="True">

Upvotes: 2

Related Questions