Johnny Prescott
Johnny Prescott

Reputation: 271

WPF: Binding not working inside ContentControl

I've been on this for hours now and I am not getting what is not working or how it is working. A bit of explanation would be appreciate how all this behave.

I'm trying to add a trigger based on another cell content and it is working fine if I forget the binding part.

My problem is actually the Binding itself. It simply doesn't work if it's inside the ContentControl.

My Code:

<GridViewColumn.CellTemplate>
    <DataTemplate>
        <ContentControl>
            <ContentControl.Style>
                <Style TargetType="{x:Type ContentControl}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding CBW_Type}" Value="Text">
                            <Setter Property="ContentTemplate">
                                <Setter.Value>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Path=CBW_Content, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
        </ContentControl>
    </DataTemplate>
</GridViewColumn.CellTemplate>

I tried the below and it work as expected (Without the trigger). The data get into place just fine.

<GridViewColumn.CellTemplate>
    <DataTemplate>
            <TextBlock Text="{Binding Path=CBW_Content, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
    </DataTemplate>
</GridViewColumn.CellTemplate>

I searched a lot of quite a few have solved this with the UpdateSourceTrigger=PropertyChanged but it didn't work for me.

Anyone can tell what is not working once I embed the binding inside the ContentControl?

Regards,

Upvotes: 1

Views: 2724

Answers (2)

mm8
mm8

Reputation: 169400

Just found the answer. I needed to add so the ContentControl is not empty I guess. If anyone can put a light on that one, I'd appreciate it.

The DataContext of the ContentTemplate of a ContentControl is the Content of the ContentControl. So for your binding to the CBW_Content property to work, you need to set or bind the Content property of the ContentControl to an instance of an object where the CBW_Content property is defined.

In this case this is the corresponding object in the ItemsSource collection of the DataGrid. That's why <ContentControl Content="{Binding}"> works.

If you don't set or bind the Content property so something, there is nothing to bind to and that's why your DataTrigger didn't work.

Hopes that makes sense.

Upvotes: 6

AnjumSKhan
AnjumSKhan

Reputation: 9827

ContentTemplate is to provide a different template for the Content, no Content => ContentTemplate clueless. So, you can also replace ContentTemplate with Content and remove DataTemplate like this in your original code :

          <Setter Property="Content">
              <Setter.Value>
                     <TextBlock Text="{Binding Path=CBW_Content, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
              </Setter.Value>
          </Setter>

Upvotes: 0

Related Questions