WPF binding to Grid.ColumnSpan

I have two text fields in gridview WPF. The second is hidden using the method on the view model (GetNoteTwoVisibility), if there is no text to display, there are no problems. In this case, although I would like to change the range of columns in the first text box to use both columns. I tried to add the GetNoteOneColumnSpan method (returning int), but this does not 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 any way to do this? Thanks

+5
source share
1 answer

You do not need another binding property for Grid.ColumnSpan. You can 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}" />
+7
source

All Articles