wpf 如何将 DataGridTemplateColumn.Visibility 绑定到 DataGrid.ItemsSource 之外的属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15310467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:59:04  来源:igfitidea点击:

How to bind DataGridTemplateColumn.Visibility to a property outside of DataGrid.ItemsSource?

wpfdata-bindingbindingwpfdatagrid

提问by Gypsy

I need to bind the Visibilityof a DataGridTemplateColumnto a property outside of the DataGrid.ItemsSource,because i need to bind this column in the all the rows to one property inside the ViewModel,but as far as i know you just can bind that to something inside the ItemsSourceor you should use ElementStyleand EditingElementStyleI've Already tried this code:

我需要绑定VisibilityDataGridTemplateColumn到的属性之外DataGrid.ItemsSource,因为我需要在所有行此列绑定到一个属性内的ViewModel,但据我所知,你才可以绑定那个东西里面ItemsSource,或者你应该使用ElementStyleEditingElementStyle我已经试过这个代码:

 <DataGridTemplateColumn Header="post" 
                      Visibility="{Binding DataContext.ProjectPostVisibility
                    , RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MvvmCommonControl:DataGrid}}"/>

And i'm Sure my binding is correct because it works fine when i bind the DataGridCell.Visibilitylike below:

而且我确定我的绑定是正确的,因为当我绑定DataGridCell.Visibility如下内容时它可以正常工作:

<DataGridTemplateColumn Header="post">
    <DataGridTemplateColumn.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Setter Property="Visibility" Value="{Binding DataContext.ProjectPostVisibility,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MvvmCommonControl:DataGrid}}"/>
                        </Style>
                    </DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn >

回答by Abdusalam Ben Haj

Your binding is correct, but it won't work with DataGridTemplateColumndirectly because it's not in the visual tree. So it's not inherting DataContext.

您的绑定是正确的,但不能DataGridTemplateColumn直接使用,因为它不在可视化树中。所以它不是继承的DataContext

You need to bind the DataGridTemplateColumnfrom code behind. Here is a demothat shows a way of doing it.

您需要绑定DataGridTemplateColumn后面的代码。这是一个演示,展示了一种方法。

回答by Bianca Kalman

Add this setter in the DataGridTemplateColumn.CellStyle and done:

在 DataGridTemplateColumn.CellStyle 中添加这个 setter 并完成:

   <Setter Property="Visibility" Value="{Binding DataContext.isVisible, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"/>

If you need more help look at my example below. I want the Remove button to not be visible at the project level. First you have to make sure you have a isVisible property in your view model:

如果您需要更多帮助,请查看我下面的示例。我希望删除按钮在项目级别不可见。首先,您必须确保您的视图模型中有一个 isVisible 属性:

  private System.Windows.Visibility _isVisible;
    public System.Windows.Visibility isVisible
    {
        get { return _isVisible; }
        set
        {
            if (_isVisible != value)
            {
                _isVisible = value;
                OnPropertyChanged("isVisible");
            }
        }
    }

Then:

然后:

  if (isProj == false)
            this.model.isVisible = Visibility.Visible;
        else
            this.model.isVisible = Visibility.Collapsed;

XAML:

XAML:

<DataGridTemplateColumn  >
       <DataGridTemplateColumn.CellTemplate >
            <DataTemplate >
               <Button x:Name="btnRemove" Content="X">
                 <Button.Style>
                    <Style TargetType="{x:Type Button}">
                         <Setter Property="FontWeight" Value="ExtraBold" />
                         <Setter Property="FontSize" Value="50" />
                     </Style>
                 </Button.Style>
            </Button>
         </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
   <DataGridTemplateColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
               <Setter Property="Background"  Value="Red"/>
               <Setter Property="Visibility" Value="{Binding DataContext.isVisible, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"/>
        </Style>
   </DataGridTemplateColumn.CellStyle>