我有DataGrid
通过数据绑定显示其行和列。我的问题是,如何清理DataGrid
的显示?例如,通过设置列width
来消除未使用的空间。通常我会为每个width
设置DataGridTextColumn
,但这次我甚至不需要在XAML中使用该行(而且它也不起作用,因为我&#39} ; m使用数据绑定填充。)
我的DataGrid
正在使用中(查看右侧的所有多余空间?):
DataGrid
的XAML:
<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:
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);
}
}
}
答案 0 :(得分:0)
如果您不自动生成列,则可以控制每列的许多属性。如果您使用width = "*"
,则它是一张外卡,将占用剩余的空间。
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding TransInfoCollection}" IsReadOnly="True" SelectedItem="{Binding SelectedTransponder}" Margin="12,12,14,60" CanUserResizeColumns="False" CanUserResizeRows="True" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Location}" Header="Location" Width="90" />
<DataGridTextColumn Binding="{Binding Path=Transponder}" Header="Transponder" Width="*" />
</DataGrid.Columns>
</DataGrid>