简体   繁体   中英

Binding Button-Visibility in Datagrid

i've defined an initial Table with three rows and 9 Column in Datagrid. right now button should be visible only if i select a row and then press another button that i defined in my Ribbon-Tab after that my Button will be Visible. sofar everything works well, but the Problem is after saving my Table, closing it and open the Table again the button is not there anymore. I set the Visibility based on if the DataGridCell.IsSelected, also a BooleanToVisibilityConverter to convert the boolean value to a Visibility one. can anyone help!

XAML:

<DataGrid.Resources>
   <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
</DataGrid.Resources>
<DataGridTemplateColumn x:Name="subgraphtyp" Header="H." Width="50">
   <DataGridTemplateColumn.CellTemplate>
     <DataTemplate>
       <Button Name="btnTable" Visibility="{Binding Path=Hinterlegung, Converter=  
               {StaticResource BoolToVisConverter}}"  Height="20" Width="25" 
               Click="Button_Table_Click">
         <Image Height="16" Source="Subgraph.png" Stretch="Fill" Width="16"/>
       </Button>
     </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

C#: public bool Hinterlegung { get; set; }

private void Button_StartTableModus(object sender, RoutedEventArgs e)
{
  if (DataGrid1.SelectedItem != null && tabItem1.IsSelected)
  {
    TableDataRowStringItem item = (TableDataRowStringItem)DataGrid1.CurrentItem;
    string wert = item.ObjectType;
    string rowName = item.Name;
    if (wert == "Function" || wert == "Process")
    {
      item.Hinterlegung = true;
      if (!tabControl.Items.Contains(tabItem2))
      {
        tabControl.Items.Add(tabItem2);
        tabItem2.Focus();
        tabItem2.IsSelected = true;
        tabItem2.Header = rowName;
        TableTab.Visibility = Visibility.Visible;
        openTabs++;
        DataGrid2.IsReadOnly = false;

        starting_Table_Mod_at_start2V();
      }
    }
  }
}

//this my initial Table
private ObservableCollection<TableDataRowStringItem> tableobject = new      
ObservableCollection<TableDataRowStringItem>();

private void starting_Table_Mod_at_start2V()
{
  List<TableDataRowStringItem> rowstringList = new List<TableDataRowStringItem>();
  TableDataRowStringItem item = new TableDataRowStringItem();
  item.RowNumber = 1; item.saveFlag = true; item.ObjectType = "E"; item.Name = "E"; 
  item.PredecessorRowNumber = "0"; rowstringList.Add(item);
  item = new TableDataRowStringItem();
  item.RowNumber = 2; item.ObjectType = "Function"; item.Name = "Function";       
  item.PredecessorRowNumber = "1"; rowstringList.Add(item);
  item = new TableDataRowStringItem();
  item.RowNumber = 3; item.ObjectType = "E"; item.Name = "E";   
  item.PredecessorRowNumber = "2"; rowstringList.Add(item);


  for (int i = 0; i < rowstringList.Count; i++)
  {
    tableobject.Add(rowstringList[i]); 
  }
  DataGrid2.ItemsSource = tableobject;
}

Your button's visibility is bound to your Hinterlegung variable which has a default value of false. So as best as I can tell, you change it to true in this method - Button_StartTableModus. But, when you reinitialize, the value reverts to false, so you need to set it to true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM