【问题标题】:WPF binding to Grid.ColumnSpanWPF 绑定到 Grid.ColumnSpan
【发布时间】:2013-04-12 12:02:09
【问题描述】:

我在 WPF 网格视图中有两个文本框。如果没有要显示的文本,则使用视图模型上的方法 (GetNoteTwoVisibility) 隐藏第二个 - 没有问题。在这种情况下,虽然我想更改第一个文本框的列跨度以使用两列。我尝试添加一个 'GetNoteOneColumnSpan' 方法(返回一个 int),但这不起作用。

<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}" />

有没有办法做到这一点?谢谢

【问题讨论】:

    标签: wpf grid wcf-binding


    【解决方案1】:

    Grid.ColumnSpan 不需要其他绑定属性。您可以尝试以下方法:

    <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}" />
    

    【讨论】:

    • 完美运行(一旦我记得我的 Visibility 方法返回了 Visibility 枚举而不是布尔值!),谢谢。
    • 欢迎您。我希望您确实有理由在 ViewModel 中使用 System.Windows.Visibility 属性,而不是在 ViewModel 中使用 bool 值并在 xaml 中使用转换器。
    • 没有理由,猜猜这只是应用程序的编写方式,将研究转换器的使用,但如果这是更好的做法,谢谢
    • 是的,双方都争辩说 System.Windows 中的东西不应该出现在 ViewModel 中,因为它有点搞砸了可测试性,有些人为了易用性而反对它。我赞成第一组哈哈。使用转换器不需要太多额外的努力,因为无论如何您都不必创建 BooleanToVisibility 转换器。可以直接使用
    猜你喜欢
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 2011-08-17
    • 2013-10-14
    • 2011-05-14
    相关资源
    最近更新 更多