views:

1640

answers:

2

I would think this is possible, but the obvious way isn't working.

Currently, I'm doing this:

<ContentControl
    Content="{Binding HurfView.EditedPart}">
    <ContentControl.Resources>
        <Style
            TargetType="ContentControl"
            x:Key="emptytemplate">
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content}"
                    Value="{x:Null}">
                    <Setter
                        Property="ContentControl.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid
                                    HorizontalAlignment="Stretch"
                                    VerticalAlignment="Stretch">
                                    <TextBlock>EMPTY!</TextBlock>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Resources>
</ContentControl>

I'm not getting any binding errors and this compiles. However, it doesn't produce the expected result. I've also tried the obvious:

<DataTemplate DataType="{x:Null}"><TextBlock>Hurf</TextBlock></DataTemplate>

This won't compile. And attempting to set the content twice fails as well:

<ContentControl
    Content="{Binding HurfView.EditedPart}">
        <TextBlock>DEFAULT DISPLAY</TextBlock>
</ContentControl>

Can I do this without writing a custom template selector?

A: 

You could return DBNull.Value as the FallbackValue of the Binding for the Content of the ContentControl, and create a DataTemplate for DBNull :

<DataTemplate DataType="{x:Type system:DBNull}">
    <!-- The default template -->
</DataTemplate>

...

<ContentControl Content="{Binding HurfView.EditedPart, FallbackValue={x:Static system:DBNull.Value}}" />
Thomas Levesque
That's kinda weird... It was my understanding the preferred method was to return [DependencyProperty.UnsetValue](http://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty.unsetvalue.aspx). I've done this in some type converters and it worked well...
Will
@Will, yes, that would probably be better. I wrote that answer more than one year ago, and my understanding of WPF at the time was still very incomplete...
Thomas Levesque
@Thomas I know the feeling...
Will
+4  A: 

Simple, you have to bind the content property in the style. Styles won't overwrite a value on a control if there's a binding present, even if the value evaluates to Null. Try this.

<ContentControl>
    <ContentControl.Style>
        <Style TargetType="ContentControl">
            <Setter Property="Content" Value="{Binding HurfView.EditedPart}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content}" Value="{x:Null}">
                    <Setter Property="ContentControl.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                    <TextBlock>EMPTY!</TextBlock>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
</ContentControl>
Bryan Anderson
Worked! Thanks.
Will
No problem it's a very common mistake people make when trying to do more complex things with styles and triggers.
Bryan Anderson