Search code examples
wpfgridwcf-binding

WPF binding to Grid.ColumnSpan


I've got two textboxes in a WPF gridview. The second one is hidden using a method on the view model (GetNoteTwoVisibility) if there is no text to display - no problem there. In this case though I'd like to change the column span of the first text box to use both columns. I've tried adding a 'GetNoteOneColumnSpan' method (returning an int) but this doesn't work.

<TextBox Name="Note1" Grid.Column="0" Text="{Binding NotesView.NoteOne}" Grid.ColumnSpan="{Binding NotesView.GetNoteColumnSpan}" />
<TextBox Name="Note2" Grid.Column="1" Text="{Binding NotesView.NoteTwo}" Visibility="{Binding NotesView.GetNoteTwoVisibility}" />

Is there a way to do this? Thanks


Solution

  • You don't need another binding property for Grid.ColumnSpan. You could try something like:

    <TextBox Name="Note1"
              Grid.Column="0"
              Text="{Binding NotesView.NoteOne}">
      <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
          <Setter Property="Grid.ColumnSpan"
                  Value="1" />
          <Style.Triggers>
            <DataTrigger Binding="{Binding NotesView.GetNoteTwoVisibility}"
                          Value="False">
              <Setter Property="Grid.ColumnSpan"
                      Value="2" />
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </TextBox.Style>
    </TextBox>
    <TextBox Name="Note2"
              Grid.Column="1"
              Text="{Binding NotesView.NoteTwo}"
              Visibility="{Binding NotesView.GetNoteTwoVisibility}" />