Put the view in a grid, instead of a stackpanel. This allows the Datagrid to stetch into parent container.

Add, Edit, Delete buttons in a horizontal stackpanel.
Changed some properties into fields.
Press delete when nothing is selected no longer crashes the UI.
pull/60/head
Stefan Roelofs 4 years ago
parent 848d1d06f9
commit 729a8a7e28

@ -3,12 +3,13 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450"
MaxWidth="800"
x:Class="UserInterface.Views.CommandSettings"> x:Class="UserInterface.Views.CommandSettings">
<StackPanel Margin="30" HorizontalAlignment="Left" >
<ContentControl FontSize="18" Margin="0 0 0 15" FontWeight="Bold">Commands</ContentControl>
<DataGrid x:Name="Grid" IsVisible="{Binding ConfiguredCommands.Count}" AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Single" Items="{Binding ConfiguredCommands}">
<Grid RowDefinitions="Auto, *, Auto, Auto" Margin="30 30 30 10" >
<TextBlock Grid.Row="0" Margin="0 0 0 15" FontSize="18" FontWeight="Bold" Text="Commands"/>
<DataGrid Grid.Row="1" x:Name="Grid" IsVisible="{Binding ConfiguredCommands.Count}"
AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Single"
Items="{Binding ConfiguredCommands}">
<DataGrid.Columns> <DataGrid.Columns>
<DataGridTextColumn Header="Name" <DataGridTextColumn Header="Name"
Binding="{Binding Name}" Binding="{Binding Name}"
@ -18,9 +19,11 @@
Width="1*" /> Width="1*" />
</DataGrid.Columns> </DataGrid.Columns>
</DataGrid> </DataGrid>
<TextBlock IsVisible="{Binding !ConfiguredCommands.Count}">Add some commands by clicking the "Add" button. </TextBlock> <TextBlock Grid.Row="2" IsVisible="{Binding !ConfiguredCommands.Count}" Text="Add some commands by clicking the 'Add' button."/>
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Width="75" HorizontalAlignment="Right" Margin="0 40 0 10" Click="Add">Add</Button> <Button Width="75" Margin="10 10 0 0" Click="AddCommand" Content="Add"/>
<Button Width="75" HorizontalAlignment="Right" Margin="0 40 0 10" Click="Delete">Delete</Button> <Button Width="75" Margin="10 10 0 0" Click="EditCommand" Content="Edit" IsVisible="False"/>
<Button Width="75" Margin="10 10 0 0" Click="DeleteCommand" Content="Delete"/>
</StackPanel> </StackPanel>
</Grid>
</UserControl> </UserControl>

@ -18,9 +18,9 @@ namespace UserInterface.Views
{ {
public class CommandSettings : UserControl public class CommandSettings : UserControl
{ {
private readonly IIpcClient<ServiceContractInterfaces> client; private readonly IIpcClient<ServiceContractInterfaces> _client;
private DataGrid _dataGrid { get; set; } private readonly DataGrid _dataGrid;
private bool sensorsNeedToRefresh { get; set; } private bool _sensorsNeedToRefresh;
public CommandSettings() public CommandSettings()
{ {
@ -35,7 +35,7 @@ namespace UserInterface.Views
.GetRequiredService<IIpcClientFactory<ServiceContractInterfaces>>(); .GetRequiredService<IIpcClientFactory<ServiceContractInterfaces>>();
// create client // create client
this.client = clientFactory.CreateClient("commands"); this._client = clientFactory.CreateClient("commands");
DataContext = new CommandSettingsViewModel(); DataContext = new CommandSettingsViewModel();
@ -44,39 +44,54 @@ namespace UserInterface.Views
this._dataGrid = this.FindControl<DataGrid>("Grid"); this._dataGrid = this.FindControl<DataGrid>("Grid");
} }
public async void GetConfiguredCommands() public async void GetConfiguredCommands()
{ {
sensorsNeedToRefresh = false; _sensorsNeedToRefresh = false;
List<ConfiguredCommandModel> status = await this.client.InvokeAsync(x => x.GetConfiguredCommands()); List<ConfiguredCommandModel> status = await this._client.InvokeAsync(x => x.GetConfiguredCommands());
((CommandSettingsViewModel)this.DataContext).ConfiguredCommands = status.Select(s => new CommandViewModel() { Name = s.Name, Type = s.Type, Id = s.Id}).ToList();
} ((CommandSettingsViewModel)this.DataContext).ConfiguredCommands = status.Select(s =>
public void Delete(object sender, RoutedEventArgs args) new CommandViewModel()
{ {
var item = ((CommandViewModel)this._dataGrid.SelectedItem); Name = s.Name,
this.client.InvokeAsync(x => x.RemoveCommandById(item.Id)); Type = s.Type,
((CommandSettingsViewModel)this.DataContext).ConfiguredCommands.Remove(item); Id = s.Id
this._dataGrid.SelectedIndex = -1; }).ToList();
((CommandSettingsViewModel)this.DataContext).TriggerUpdate();
} }
public async void Add(object sender, RoutedEventArgs args) public async void AddCommand(object sender, RoutedEventArgs args)
{ {
AddCommandDialog dialog = new AddCommandDialog(); var dialog = new AddCommandDialog();
if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{ {
await dialog.ShowDialog(desktop.MainWindow); await dialog.ShowDialog(desktop.MainWindow);
sensorsNeedToRefresh = true; _sensorsNeedToRefresh = true;
GetConfiguredCommands(); GetConfiguredCommands();
} }
} }
public void EditCommand(object sender, RoutedEventArgs args)
{
}
public void DeleteCommand(object sender, RoutedEventArgs args)
{
if (_dataGrid.SelectedItem is not CommandViewModel item)
return;
this._client.InvokeAsync(x => x.RemoveCommandById(item.Id));
if (DataContext is not CommandSettingsViewModel viewModel)
return;
viewModel.ConfiguredCommands.Remove(item);
_dataGrid.SelectedIndex = -1;
viewModel.TriggerUpdate();
}
private void InitializeComponent() private void InitializeComponent()
{ {
AvaloniaXamlLoader.Load(this); AvaloniaXamlLoader.Load(this);
} }
} }
} }

@ -3,12 +3,13 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450"
MaxWidth="800"
x:Class="UserInterface.Views.SensorSettings"> x:Class="UserInterface.Views.SensorSettings">
<StackPanel Margin="30" HorizontalAlignment="Left" >
<ContentControl FontSize="18" Margin="0 0 0 15" FontWeight="Bold">Sensors</ContentControl>
<DataGrid x:Name="Grid" IsVisible="{Binding ConfiguredSensors.Count}" AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Single" Items="{Binding ConfiguredSensors}">
<Grid RowDefinitions="Auto, *, Auto, Auto" Margin="30 30 30 10" >
<TextBlock Grid.Row="0" Margin="0 0 0 15" FontSize="18" FontWeight="Bold" Text="Sensors"/>
<DataGrid Grid.Row="1" x:Name="Grid" IsVisible="{Binding ConfiguredSensors.Count}"
AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Single"
Items="{Binding ConfiguredSensors}">
<DataGrid.Columns> <DataGrid.Columns>
<DataGridTextColumn Header="Name" <DataGridTextColumn Header="Name"
Binding="{Binding Name}" Binding="{Binding Name}"
@ -24,9 +25,11 @@
Width="2*" /> Width="2*" />
</DataGrid.Columns> </DataGrid.Columns>
</DataGrid> </DataGrid>
<TextBlock IsVisible="{Binding !ConfiguredSensors.Count}">Add some sensors by clicking the "Add" button. </TextBlock> <TextBlock Grid.Row="2" IsVisible="{Binding !ConfiguredSensors.Count}" Text="Add some sensors by clicking the 'Add' button."/>
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Width="75" HorizontalAlignment="Right" Margin="0 40 0 10" Click="AddSensor">Add</Button> <Button Width="75" Margin="10 10 0 0" Click="AddSensor" Content="Add"/>
<Button Width="75" HorizontalAlignment="Right" Margin="0 40 0 10" Click="Delete">Delete</Button> <Button Width="75" Margin="10 10 0 0" Click="EditSensor" Content="Edit" IsVisible="False"/>
<Button Width="75" Margin="10 10 0 0" Click="DeleteSensor" Content="Delete"/>
</StackPanel> </StackPanel>
</Grid>
</UserControl> </UserControl>

@ -18,9 +18,9 @@ namespace UserInterface.Views
{ {
public class SensorSettings : UserControl public class SensorSettings : UserControl
{ {
private readonly IIpcClient<ServiceContractInterfaces> client; private readonly IIpcClient<ServiceContractInterfaces> _client;
private DataGrid _dataGrid { get; set; } private readonly DataGrid _dataGrid;
private bool sensorsNeedToRefresh { get; set; } private bool _sensorsNeedToRefresh;
public SensorSettings() public SensorSettings()
{ {
@ -35,7 +35,7 @@ namespace UserInterface.Views
.GetRequiredService<IIpcClientFactory<ServiceContractInterfaces>>(); .GetRequiredService<IIpcClientFactory<ServiceContractInterfaces>>();
// create client // create client
this.client = clientFactory.CreateClient("sensors"); this._client = clientFactory.CreateClient("sensors");
DataContext = new SensorSettingsViewModel(); DataContext = new SensorSettingsViewModel();
@ -44,21 +44,31 @@ namespace UserInterface.Views
this._dataGrid = this.FindControl<DataGrid>("Grid"); this._dataGrid = this.FindControl<DataGrid>("Grid");
} }
public async void GetConfiguredSensors() public async void GetConfiguredSensors()
{ {
sensorsNeedToRefresh = false; _sensorsNeedToRefresh = false;
List<ConfiguredSensorModel> status = await this.client.InvokeAsync(x => x.GetConfiguredSensors()); List<ConfiguredSensorModel> status = await this._client.InvokeAsync(x => x.GetConfiguredSensors());
((SensorSettingsViewModel)this.DataContext).ConfiguredSensors = status.Select(s => new SensorViewModel() { Name = s.Name, Type = s.Type, Value = s.Value, Id = s.Id, UpdateInterval = s.UpdateInterval, UnitOfMeasurement = s.UnitOfMeasurement }).ToList(); ((SensorSettingsViewModel)this.DataContext).ConfiguredSensors = status.Select(s =>
while (!sensorsNeedToRefresh) new SensorViewModel()
{
Name = s.Name,
Type = s.Type,
Value = s.Value,
Id = s.Id,
UpdateInterval = s.UpdateInterval,
UnitOfMeasurement = s.UnitOfMeasurement
}).ToList();
while (!_sensorsNeedToRefresh)
{ {
await Task.Delay(1000); await Task.Delay(1000);
List<ConfiguredSensorModel> statusUpdated = await this.client.InvokeAsync(x => x.GetConfiguredSensors()); List<ConfiguredSensorModel> statusUpdated = await this._client.InvokeAsync(x => x.GetConfiguredSensors());
var configuredSensors = ((SensorSettingsViewModel)this.DataContext).ConfiguredSensors; var configuredSensors = ((SensorSettingsViewModel)this.DataContext).ConfiguredSensors;
// this is a workaround for the list showing before it has been completely loaded in the service // this is a workaround for the list showing before it has been completely loaded in the service
if (statusUpdated.Count != configuredSensors.Count) { if (statusUpdated.Count != configuredSensors.Count)
sensorsNeedToRefresh = true; {
_sensorsNeedToRefresh = true;
GetConfiguredSensors(); GetConfiguredSensors();
} }
statusUpdated.ForEach(s => statusUpdated.ForEach(s =>
@ -72,33 +82,42 @@ namespace UserInterface.Views
} }
}); });
} }
}
public void Delete(object sender, RoutedEventArgs args)
{
var item = ((SensorViewModel)this._dataGrid.SelectedItem);
this.client.InvokeAsync(x => x.RemoveSensorById(item.Id));
((SensorSettingsViewModel)this.DataContext).ConfiguredSensors.Remove(item);
this._dataGrid.SelectedIndex = -1;
((SensorSettingsViewModel)this.DataContext).TriggerUpdate();
} }
public async void AddSensor(object sender, RoutedEventArgs args) public async void AddSensor(object sender, RoutedEventArgs args)
{ {
AddSensorDialog dialog = new AddSensorDialog(); var dialog = new AddSensorDialog();
if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{ {
await dialog.ShowDialog(desktop.MainWindow); await dialog.ShowDialog(desktop.MainWindow);
sensorsNeedToRefresh = true; _sensorsNeedToRefresh = true;
GetConfiguredSensors(); GetConfiguredSensors();
} }
} }
private void InitializeComponent() public void EditSensor(object sender, RoutedEventArgs args)
{ {
AvaloniaXamlLoader.Load(this);
} }
public void DeleteSensor(object sender, RoutedEventArgs args)
{
if (_dataGrid.SelectedItem is not SensorViewModel item)
return;
this._client.InvokeAsync(x => x.RemoveSensorById(item.Id));
if (DataContext is not SensorSettingsViewModel viewModel)
return;
viewModel.ConfiguredSensors.Remove(item);
_dataGrid.SelectedIndex = -1;
viewModel.TriggerUpdate();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
} }
} }
Loading…
Cancel
Save