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

倾然丶 夕夏残阳落幕 提交于 2019-12-09 02:29:37

问题


I need to bind the Visibility of a DataGridTemplateColumn to 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 ItemsSource or you should use ElementStyle and EditingElementStyle I've Already tried this code:

 <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.Visibility like below:

<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 >

回答1:


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

You need to bind the DataGridTemplateColumn from code behind. Here is a demo that shows a way of doing it.




回答2:


Add this setter in the DataGridTemplateColumn.CellStyle and done:

   <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:

  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:

<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>



来源:https://stackoverflow.com/questions/15310467/how-to-bind-datagridtemplatecolumn-visibility-to-a-property-outside-of-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!