EBDN - Community - Question & Answers

  Monday, 05 August 2019
  1 Replies
  1.3K Visits
0
Votes
Undo
Hi,

Attached image1 is the data grid table with checkbox and a combo box for each user. The problem is, when i select the item from combo box for each user and click the button Add, MySelected item is not returning any selected value even when the checkbox is true. It only returns the selected value if i double click on the user textblock of the datagridcell.

Is there any way to get the selected value without having to double click on each textblock of the datagridcell ?

Below are my codes:

UserWindow.xaml

<Window x:Class="TokenLicenseUsageReport.UserWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TokenLicenseUsageReport"
xmlns:System="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d" ResizeMode="NoResize" Width="470" Height="520" Title="EB Flexnet License Tracking Tool" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="White"/>
<GradientStop Color="WhiteSmoke" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<DataGrid Width="410" Name="dataGridView1" SelectedCellsChanged="DataGrid_SelectedCellsChanged" VirtualizingStackPanel.IsVirtualizing="False" Background="White" BorderBrush="DarkGray" Height="273" ColumnHeaderHeight="23" CanUserAddRows="False" HorizontalAlignment="Center" Margin="24,140,24,0" VerticalAlignment="Top" ItemsSource="{Binding MyData}" AutoGenerateColumns="False" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="" Binding="{Binding Checkbox, Mode=TwoWay}" Width="25">
</DataGridCheckBoxColumn>
<DataGridTextColumn Header=" User" Binding="{Binding User, Mode=TwoWay}" Width="190" >
</DataGridTextColumn>
<DataGridTemplateColumn Header=" Group" Width="180">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Margin="2" Height="30" Text="Select Group" SelectedItem="{Binding SelGroup, Mode=TwoWay}" ItemsSource="{Binding Group}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding GroupName}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Label Content="The following users are not listed in the User and Groups list.
Please select the respective group for each users." HorizontalAlignment="Left" Margin="22,10,0,0" VerticalAlignment="Top" Height="49" Width="419"/>
<Button Command="{Binding CmdRun}" BorderBrush="#FFA3D9F6" BorderThickness="2" Content="Add" Height="23" HorizontalAlignment="Left" Margin="280,437,0,0" VerticalAlignment="Top" Width="75">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFFFF"/>
<GradientStop Color="#FFCCE6F4" Offset="0.84"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button Content="Close" BorderThickness="1.35" BorderBrush="DarkGray" Command="{Binding CmdCancel}" Height="23" HorizontalAlignment="Left" Margin="360,436,0,0" VerticalAlignment="Top" Width="76">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFFFFF"/>
<GradientStop Color="#FFD4D4D4" Offset="0.84"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<TextBox Background="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" BorderThickness="1.25" BorderBrush="LightGray" HorizontalAlignment="Left" Height="23" Margin="24,83,0,0" TextWrapping="Wrap" Text="{Binding NewGroup}" VerticalAlignment="Top" Width="335"/>
<Button Command="{Binding CmdAdd}" BorderBrush="#FFA3D9F6" BorderThickness="2" Content="Add" HorizontalAlignment="Left" Margin="364,83,0,0" VerticalAlignment="Top" Width="76" Height="22">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFFFF"/>
<GradientStop Color="#FFCCE6F4" Offset="0.84"/>
</LinearGradientBrush>
</Button.Background>
</Button>
</Grid>
</Window>


UserWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;



namespace TokenLicenseUsageReport
{

/// <summary>
/// Interaction logic for UserWindow.xaml
/// </summary>
public partial class UserWindow : Window
{

public UserWindow()
{
InitializeComponent();

}

private void DataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
var vm = DataContext as VmUserWindow;

vm.MySelectedItem.Checkbox = true;


}

}
}


VmUserWindow.cs

using Aucotec.EngineeringBase.Client.Runtime;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Data;
using System.Windows.Input;
using Excel = Microsoft.Office.Interop.Excel;
namespace TokenLicenseUsageReport
{
public class VmUserWindow : Helpers.VmBase //and this is the class2
{
private ObservableCollection<ModelClass> _group2;
public ObservableCollection<ModelClass> Group2
{
get { return _group2; }
set
{
_group2 = value;
OnPropertyChanged("Group2";);
}
}
private ObservableCollection<ModelClass> _MyData;
public ObservableCollection<ModelClass> MyData
{
get { return _MyData; }
set
{
_MyData = value;
OnPropertyChanged("MyData";);
}
}
public HashSet<string> debug2;
public string FilePlace { get; set; }
public ObjectCollection TokenTemplate;
public Application myApplication;
public ICommand CmdRun { get; private set; }
public ICommand CmdCancel { get; private set; }
public ICommand CmdAdd { get; private set; }
public ModelClass MySelectedItem { get; set; }
public UserWindow user;
public HashSet<string> debug;
public HashSet<AddtoExcel> addtoexcel;
public ICollectionView Customers { get; set; }
public ICollectionView GroupedCustomers { get; set; }
public List<VmDateWindow.GroupFile> GroupFiles;
public string NewGroup { get; set; }
public struct AddtoExcel
{
public string member { get; set; }
public string group { get; set; }
}
public struct GetRow
{
public int RowID { get; set; }
public int ColID { get; set; }
public string Group { get; set; }
public string Member { get; set; }
}

public VmUserWindow(UserWindow user, HashSet<string> debug, List<VmDateWindow.GroupFile> GroupFiles, Application myApplication, string FilePlace)
{
MyData = new ObservableCollection<ModelClass>();
CmdAdd = new Helpers.RelayCommand(ExeAdd);
this.FilePlace = FilePlace;
CmdRun = new Helpers.RelayCommand(ExeRun);
CmdCancel = new Helpers.RelayCommand(ExeCancel);
this.user = user;
this.myApplication = myApplication;
addtoexcel = new HashSet<AddtoExcel>();

debug2 = new HashSet<string>();
Group2 = new ObservableCollection<ModelClass>();

var SortedGroupName = GroupFiles.Select(x => x.GroupName).Distinct().ToList();

foreach (var item in SortedGroupName)
{
Group2.Add(new ModelClass() { GroupName = item });

}




foreach (var item in debug)
{
MyData.Add(new ModelClass() { User = item, Group = Group2, Checkbox = true });
}


}

private void ExeAdd(object obj)
{
Group2.Add(new ModelClass() { GroupName = NewGroup});


}

private void ExeCancel(object obj)
{
user.Visibility = System.Windows.Visibility.Hidden;
}


private void ExeRun(object obj)
{

foreach (ModelClass MySelectedItem in MyData)
{
if (MySelectedItem.Checkbox == true)
{
try
{
System.Windows.MessageBox.Show(MySelectedItem.User + " " + MySelectedItem.SelGroup.GroupName);
}
catch (Exception exp)
{

}



}
}


}


}
}


Thank you.

Regards,
Yuvarani