Datagrid中的绑定按钮可见性

时间:2012-09-13 13:30:24

标签: c# wpf datagrid wpf-controls wpfdatagrid

我在Datagrid中定义了一个包含三行和9列的初始表。现在只有当我选择一行然后再按我在我的功能区选项卡中定义的另一个按钮后,按钮应该是可见的,之后我的按钮将是可见的。 sofare一切运作良好,但问题是在保存我的表,关闭它并再次打开表后按钮不再存在。我根据DataGridCell.IsSelected设置了Visibility,也是一个BooleanToVisibilityConverter,将布尔值转换为Visibility值。 任何人都可以帮忙!

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;组; }

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

1 个答案:

答案 0 :(得分:0)

您的按钮的可见性与您的Hinterlegung变量绑定,该变量的默认值为false。所以尽我所知,你在这个方法中将它改为true - Button_StartTableModus。但是,当您重新初始化时,该值将恢复为false,因此您需要将其设置为true。