WPF DataGridColumn条件控件

时间:2014-05-12 14:55:07

标签: c# wpf datagridcolumn

我目前正在数据网格中显示一个超链接,其中包含以下DataGridHyperLinkColumn定义:

  <DataGridHyperlinkColumn Header="Item" Binding="{Binding Item, Mode=OneWay}">
    <DataGridHyperlinkColumn.ElementStyle>
      <Style>
        <EventSetter Event="Hyperlink.Click" Handler="ButtonItemInfo_OnClick"/>
      </Style>
    </DataGridHyperlinkColumn.ElementStyle>
  </DataGridHyperlinkColumn>

我想将其更改为有条件地显示超链接或标签(或文本块)。所以如果绑定值是“SH”,我想显示标签。否则我想要超链接。

我该如何做到这一点?

1 个答案:

答案 0 :(得分:1)

我会将超链接绑定到命令,并在CanExecute中有条件地返回false。您可以为超级用户设置禁用状态的样式。

<DataGrid ItemsSource="{Binding Items}">
  <DataGrid.Columns>
    <DataGridTemplateColumn Header="Item">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <TextBlock>
            <Hyperlink Command="{Binding DataContext.Navigate, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                       CommandParameter="{Binding}">                  
                 <TextBlock Text="{Binding}" />
            </Hyperlink>
        </TextBlock>
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>
</DataGrid>

ViewModel:

    public ObservableCollection<string> Items
    {
        get;
        set;
    }

    public ICommand Navigate
    {
        get
        {
            return new RelayCommand(
                (param) => DoNavigate(param as string), // execute
                (param) =>                              // can execute
                {
                    var link = param as string;
                    return link != "SH";
                });
        }
    }

如果您确实需要某些行的文本框,可以在列格式样式中使用DataTrigger。

<DataGrid ItemsSource="{Binding Items}">
  <DataGrid.Columns>
    <DataGridTemplateColumn Header="Item">
      <DataGridTemplateColumn.CellStyle>
        <Style TargetType="DataGridCell">
          <Setter Property="Template">
            <Setter.Value>
             <!-- Template for normal rows -->
              <ControlTemplate>
                <TextBlock>
                  <Hyperlink Command="{Binding DataContext.Navigate, 
                               RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                             CommandParameter="{Binding}">                  
                    <TextBlock Text="{Binding}" />
                  </Hyperlink>
                </TextBlock>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
          <Style.Triggers>
            <DataTrigger Binding="{Binding}" Value="SH">
              <Setter Property="Template">
                <Setter.Value>
                  <!-- Template for SH rows -->
                  <ControlTemplate>
                    <TextBlock Text="{Binding}" />
                  </ControlTemplate>
                </Setter.Value>
              </Setter>
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </DataGridTemplateColumn.CellStyle>

    </DataGridTemplateColumn>
  </DataGrid.Columns>
</DataGrid>