0

In my DataGridTemplateColumn i set a DataTemplate.

Inside i have a Button and i bind his Visibility property to collapsed but the result leave space like Hidden. why?

If I just write "Collapsed" it's works but i want to bind. Maybe i didn't write the binding perfect? Please help

 <DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate >
       <DataTemplate>
           <StackPanel  Width="auto" Orientation="Horizontal" >
              <Button Click="DeleteTravelDetails"  Visibility="{Binding DataContext.IsDeleteTravelVisible,RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}">
                <Image Source="/Resources;component/Images/delete3.jpg" Width="auto" Height="30" ToolTip="Delete Travel"  />
              </Button>
              <Button Click="ExpensesTravelClick" Margin="4,0,0,0" >
                 <Image Source="/Resources;component/Images/information.png" Width="auto" Height="30"/>
              </Button>
              <Button Click="ExcelTravelClick" Margin="4,0,0,0" >
                 <Image Source="/Resources;component/Images/excel.jpg" Width="auto" Height="30"/>
              </Button>
           </StackPanel>
        </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>        
  </DataGridTemplateColumn>

sample

Code

private Visibility _isDeleteTravelVisible = Visibility.Collapsed; 
public Visibility IsDeleteTravelVisible { 
    get { return _isDeleteTravelVisible; } 
    set { _isDeleteTravelVisible = value; 
         if (PropertyChanged != null) { 
            PropertyChanged(this, new PropertyChangedEventArgs("IsDeleteTravelVisible")); 
            }
        }
    }
Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40
  • Make sure that DataContext.IsDeleteTravelVisible is a Visibility property and not a bool one. – Giangregorio Sep 07 '16 at 08:02
  • Hi Giangredorio. this property is visibilty property, but this not remove the spcae. why? private Visibility _isDeleteTravelVisible = Visibility.Collapsed; public Visibility IsDeleteTravelVisible { get { return _isDeleteTravelVisible; } set { _isDeleteTravelVisible = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("IsDeleteTravelVisible")); }}} – Liran Kremer Sep 07 '16 at 08:05
  • 1
    @LiranKremer, if delete button was `Hidden` not `Collapsed`, then there would empty space before Information button, not after Excel button – ASh Sep 07 '16 at 08:08
  • Thanks for all i solved the problem from this link https://stackoverflow.com/questions/20174094/datagrid-catch-cell-value-changed-event-with-a-single-click-on-updatesourcetrigg/20175265#20175265 – Liran Kremer Sep 07 '16 at 14:24

2 Answers2

0

The width of your DataGrid cell in this case depends on the width of the DataGrid column. In your case the visibility property is indeed set to collapsed, the StackPanel width is defined as auto, so it is smaller even though it's not visible, but the column width dictates the width of the cell that contains your stackpanel.

https://msdn.microsoft.com/en-us/library/cc903935(v=vs.95).aspx link contains the sizing information for DataGrid, take a look at the "Sizing Columns and Column Headers" section

shwick
  • 105
  • 1
  • 9
0

As @Ivan Milenkovic mentioned the size you see is depending on the gridcolumn.

Here in this stackoverflow answer you can see how to update the column size when there has been changes to your column or the button visibility: https://stackoverflow.com/a/5651287/2289942

Community
  • 1
  • 1
Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40