I have a DataGrid
that displays its rows and columns through data binding. My question is, how do I clean up the display of my DataGrid
? For example, getting rid of unused space by setting the column width
. Usually I would set the width
for every DataGridTextColumn
, but this time I don't even need to use that line in the XAML(and it also doesn't work because I'm populating using data binding).
My DataGrid
in use (See all the excess space on the right?):
XAML of the DataGrid
:
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding TransInfoCollection}" IsReadOnly="True" SelectedItem="{Binding SelectedTransponder}" Margin="12,12,14,60" CanUserResizeColumns="False" CanUserResizeRows="True" >
<DataGrid.Columns>
<DataGridTextColumn Header="{DynamicResource transLocation}" Binding="{Binding Path=Location}" Width="90" />
<DataGridTextColumn Header="{DynamicResource transponder}" Binding="{Binding Path=Tranponder}" Width="*" />
</DataGrid.Columns>
</DataGrid>
ViewModel of the window:
public class TransponderMaintenanceViewModel : PropertyChangedBase
{
private ObservableCollection<TransponderProps> _transInfoCollection; //DataGrid Collection
private TransponderProps _selectedTransponder;
private Command _deleteCommand;
HierarchicalVM pathLocationsNode = App.MainTree.Data[2].Children[0]; //Path -> Locations
public TransponderMaintenanceViewModel()
{
TransInfoCollection = new ObservableCollection<TransponderProps>();
_deleteCommand = new Command(DeleteCommand_Ops);
for (int i = 0; i < pathLocationsNode.Children.Count(); i++) //Adds Transponder information to DataGrid
{
if (pathLocationsNode.Children[i].Children[0].Children.Count() > 0)
{
for (int j = 0; j < pathLocationsNode.Children[i].Children[0].Children.Count(); j++)
{
TransInfoCollection.Add(new TransponderProps { Location = new MultiItemString(new[] { Regex.Match(pathLocationsNode.Children[i].DisplayName.Value, @"\d+").Value }, false),
Transponder = new MultiItemString(new[] { pathLocationsNode.Children[i].Children[0].Children[j].DisplayName.Value }, false) });
}
}
}
}
//Prop for Collection that will store Transponders and their Location
public ObservableCollection<TransponderProps> TransInfoCollection
{
get { return _transInfoCollection; }
set
{
_transInfoCollection = value;
NotifyPropertyChange(() => TransInfoCollection);
}
}
//Prop for the SelectedItem in the DataGrid
public TransponderProps SelectedTransponder
{
get { return _selectedTransponder; }
set
{
_selectedTransponder = value;
NotifyPropertyChange(() => SelectedTransponder);
}
}
//Delete Command
public Command DeleteCommand { get { return _deleteCommand; } }
private void DeleteCommand_Ops()
{
for (int i = 0; i < pathLocationsNode.Children.Count(); i++) //Delete Transponder from CartTools
{
for (int j = 0; j < pathLocationsNode.Children[i].Children[0].Children.Count(); j++)
{
if (pathLocationsNode.Children[i].Children[0].Children[j].DisplayName.Value == SelectedTransponder.Transponder.Value)
{
var transParent = pathLocationsNode.Children[i].Children[0];
transParent.Children.Remove(transParent.Children[j]);
}
}
}
TransInfoCollection.Remove(SelectedTransponder); //Remove Transponder from DataGrid
}
}
public class TransponderProps : PropertyChangedBase
{
private MultiItemString _location, _transponder;
public TransponderProps()
{
}
public MultiItemString Location
{
get { return _location; }
set
{
_location = value;
NotifyPropertyChange(() => Location);
}
}
public MultiItemString Transponder
{
get { return _transponder; }
set
{
_transponder = value;
NotifyPropertyChange(() => Transponder);
}
}
}