Display a default DataTemplate in a ContentControl when its content is null or empty?

后端 未结 3 852
逝去的感伤
逝去的感伤 2020-12-13 03:48

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

Currently, I\'m doing this:



        
相关标签:
3条回答
  • 2020-12-13 04:32

    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>
    
    0 讨论(0)
  • 2020-12-13 04:34

    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}}" />
    
    0 讨论(0)
  • 2020-12-13 04:40

    Since I stumbled upon this question and had the same problem today, I wanted to contribute another way how I solved the problem. Since I did not like to add another style trigger I used the property TargetNullValue which seems to be a bit more readable than the accepted solution (which works nevertheless):

        <ContentControl>
          <ContentControl.Content>
            <Binding Path="ContentViewModel">
              <Binding.TargetNullValue>
                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                  <TextBlock>EMPTY!</TextBlock>
                </Grid>
              </Binding.TargetNullValue>
            </Binding>
          </ContentControl.Content>
        </ContentControl>
    
    0 讨论(0)
提交回复
热议问题