stigzler
stigzler
CC#
Created by stigzler on 6/12/2024 in #help
Community.Toolkit.MVVM Error on [ObservableProperty]: The name 'x' does not exist in current context
No description
21 replies
CC#
Created by stigzler on 6/7/2024 in #help
WPF - Unable to create copy of template in Visual Studio
No description
1 replies
CC#
Created by stigzler on 5/26/2024 in #help
Filtering an Observable Collection in WPF MVVM via ICollectionView
I have an observabelCollection of a data object:
public ObservableCollection<GistViewModel> Gists { get => gists; set => SetProperty(ref gists, value); }
public ObservableCollection<GistViewModel> Gists { get => gists; set => SetProperty(ref gists, value); }
Each GistViewModel has the following:
public BindingList<GistFileViewModel> GistFiles { get => gistFiles; }
public BindingList<GistFileViewModel> GistFiles { get => gistFiles; }
In terms of what this represents, each Gist has a collection of GistFiles. I am mapping this onto a TreeViewControl (root being Gists, Child being GistFiles) via the following xaml (abridged):
<!-- GistLevel -->
<HierarchicalDataTemplate DataType="{x:Type viewmodels:GistViewModel}" ItemsSource="{Binding Path=GistsFiles}">

<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding GistFiles, Converter={StaticResource GistFilesToFirstFilenameConverter}}"></TextBlock>
</StackPanel>

</HierarchicalDataTemplate>

<!-- GIST FILE LEVEL -->

<DataTemplate DataType="{x:Type viewmodels:GistFileViewModel}">
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Margin="2,0,2,0" Text="{Binding Filename}"></TextBlock>
</StackPanel>
</DataTemplate>

<TreeView x:Name="GistsTV" ItemsSource="{Binding Gists}"/>
<!-- GistLevel -->
<HierarchicalDataTemplate DataType="{x:Type viewmodels:GistViewModel}" ItemsSource="{Binding Path=GistsFiles}">

<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding GistFiles, Converter={StaticResource GistFilesToFirstFilenameConverter}}"></TextBlock>
</StackPanel>

</HierarchicalDataTemplate>

<!-- GIST FILE LEVEL -->

<DataTemplate DataType="{x:Type viewmodels:GistFileViewModel}">
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Margin="2,0,2,0" Text="{Binding Filename}"></TextBlock>
</StackPanel>
</DataTemplate>

<TreeView x:Name="GistsTV" ItemsSource="{Binding Gists}"/>
This works successfully. Now I am trying to introduce a filter feature. I have followed a guide online somewhere which advises using ICollectionView. I implement this thus:
public ICollectionView GistsView { get => CollectionViewSource.GetDefaultView(Gists); }

private async Task GetAllGistsAsync()
{
Gists = await gistManager.LoadGistsAsync();
GistsView.Refresh();
}
public ICollectionView GistsView { get => CollectionViewSource.GetDefaultView(Gists); }

private async Task GetAllGistsAsync()
{
Gists = await gistManager.LoadGistsAsync();
GistsView.Refresh();
}
And changing my xaml to:
<TreeView x:Name="GistsTV" ItemsSource="{Binding GistsView}"/>
<TreeView x:Name="GistsTV" ItemsSource="{Binding GistsView}"/>
I'm just trying to get the default list presented first. However, this does not work. No list items are displayed. GistFilesToFirstFilenameConverter doesn't get called when Binding = GistsView The full code is here: https://github.com/stigzler/VisGist/blob/master/VisGist/ToolWindows/MainWindow.xaml What am I missing?
3 replies
CC#
Created by stigzler on 5/20/2024 in #help
✅ Force Binding update in ViewModel where UpdateSourceTrigger==LostFocus
Hi folks - sorry to keep posting, but WPF is proving a difficult mare to break in.... I have a text editor control whose text is bound to a ViewModel Property and UpdateSourceTrigger set to LostFocus It's not possible to set it to PropertyChanged because it compares the text against a default text set which can be really long. If set to PropertyChanged, it introduces too much lag. Relevant code: View:
<syncfusion:EditControl x:Name="CodeEC" Grid.Row="4"
Text="{Binding SelectedGistFileViewModel.Content, Mode=TwoWay, UpdateSourceTrigger=LostFocus}
</syncfusion:EditControl>
<syncfusion:EditControl x:Name="CodeEC" Grid.Row="4"
Text="{Binding SelectedGistFileViewModel.Content, Mode=TwoWay, UpdateSourceTrigger=LostFocus}
</syncfusion:EditControl>
ViewModel:
public GistFileViewModel SelectedGistFileViewModel { get => selectedGistFileViewModel; set => SetProperty(ref selectedGistFileViewModel, value); }
public GistFileViewModel SelectedGistFileViewModel { get => selectedGistFileViewModel; set => SetProperty(ref selectedGistFileViewModel, value); }
GistFileViewModel :
public string Content {
get { return content; }
set
{
if (value != ReferenceGistFile.Content) HasChanges = true;
else HasChanges = false;

SetProperty(ref content, value);
ParentGistViewModel.HasChanges = ParentGistViewModel.GistHasChanges();
}
}
public string Content {
get { return content; }
set
{
if (value != ReferenceGistFile.Content) HasChanges = true;
else HasChanges = false;

SetProperty(ref content, value);
ParentGistViewModel.HasChanges = ParentGistViewModel.GistHasChanges();
}
}
The issue is, I have a manual Save button. If I click this, the text control does not seem to loose focus and thus the bound property does not get updated. I'm trying to avoid View code behind as much as possible. The Save button click calls a Command in the ViewModel. How can I go about manually updating either the Property, or forcing the text control to lose focus? Tried a few bit scraped from the interweb, but no joy.
2 replies
CC#
Created by stigzler on 5/19/2024 in #help
✅ How to use IRelayCommand in CommunityToolkit.Mvvm with parameters?
I'm just learning WPF and also the community toolkit. I can get RelayCommands working OK if they're parameter-less, but struggling to implement a command bound to a Toggle Button to toggle a bool property on a databound control. My working approach to simple commands:
public IRelayCommand LogOutCMD { get; set; }
private void SetupCommands() // this is called at startup
{
LogOutCMD = new RelayCommand(LogOut);
}
private void LogOut()
{
// does stuff
}
public IRelayCommand LogOutCMD { get; set; }
private void SetupCommands() // this is called at startup
{
LogOutCMD = new RelayCommand(LogOut);
}
private void LogOut()
{
// does stuff
}
However, I have tried something similar for the ToggleButton:
public bool CodeNumberingVisible { get => codeNumberingVisible; set => SetProperty(ref codeNumberingVisible, value); }
public IRelayCommand SetCodeNumberingVisibleCMD { get; set; }
SetCodeNumberingVisibleCMD = new RelayCommand<bool>(SetCodeNumberingVisible);
private void SetCodeNumberingVisible(bool visible)
{
CodeNumberingVisible = visible;
}
public bool CodeNumberingVisible { get => codeNumberingVisible; set => SetProperty(ref codeNumberingVisible, value); }
public IRelayCommand SetCodeNumberingVisibleCMD { get; set; }
SetCodeNumberingVisibleCMD = new RelayCommand<bool>(SetCodeNumberingVisible);
private void SetCodeNumberingVisible(bool visible)
{
CodeNumberingVisible = visible;
}
The button and control xaml:
<ToggleButton x:Name="CodeNumberVisibleBT" ToolTip="Toggle Code Numbers"
Command="{Binding SetCodeNumberingVisibleCMD}"
IsChecked="{Binding CodeNumberingVisible}"
CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}"/>
<syncfusion:EditControl ShowLineNumber="{Binding CodeNumberingVisible}"/>
<ToggleButton x:Name="CodeNumberVisibleBT" ToolTip="Toggle Code Numbers"
Command="{Binding SetCodeNumberingVisibleCMD}"
IsChecked="{Binding CodeNumberingVisible}"
CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}"/>
<syncfusion:EditControl ShowLineNumber="{Binding CodeNumberingVisible}"/>
However, this renders the button inoperable (no rollover highlighting, command doesn't execute) - it works without the Command Binding. Any ideas? EDIT: I've just realised that I don't technically need a Command here, I can just bind the ToggleButton's IsChecked property to the property directly. However, I'm still intersted to know why the above broke the ToggleButton and how to send parameters via cmmands using the Community Toolkit.
77 replies
CC#
Created by stigzler on 5/5/2024 in #help
✅ Could not load file or assembly or one of its dependencies at runtime despite being present
No description
68 replies
CC#
Created by stigzler on 5/5/2024 in #help
✅ WPF - Accessing a TreeViewItem's root UserControl DataContext for a Command
I am trying to execute a Command which exists in the ViewModel bound to the root parent UserControl of a child TreeViewItem. This is from a ContextMenu bound to root TreeViewItems. The TreeView code: Key code:
<StackPanel.ContextMenu>
<ContextMenu Style="{StaticResource ContextMenu}">
<MenuItem Header="Delete Gist" Background="{DynamicResource {x:Static vsshell:VsBrushes.WindowKey}}"
Style="{StaticResource MenuItem}"
Command="{Binding
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type viewmodels:MainWindowViewModel}},
Path=DataContext.DeleteGistCMD}">
<MenuItem.Icon>
<imaging:CrispImage Moniker="{x:Static catalog:KnownMonikers.DeleteListItem}"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</StackPanel.ContextMenu>
<StackPanel.ContextMenu>
<ContextMenu Style="{StaticResource ContextMenu}">
<MenuItem Header="Delete Gist" Background="{DynamicResource {x:Static vsshell:VsBrushes.WindowKey}}"
Style="{StaticResource MenuItem}"
Command="{Binding
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type viewmodels:MainWindowViewModel}},
Path=DataContext.DeleteGistCMD}">
<MenuItem.Icon>
<imaging:CrispImage Moniker="{x:Static catalog:KnownMonikers.DeleteListItem}"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</StackPanel.ContextMenu>
Broader context: https://paste.mod.gg/khruicdqkfhy/1 MainWindowViewModel is the DataContext of MainWindow, within which the Command I'm targeting exists. I've tried various permutations of RelativeSource for Binding, as despite xaml intellisense suggesting the binding is right by offering the command as an option, it doesn't fire on selecting the relevant ContextMenuItem. Any ideas?
2 replies
CC#
Created by stigzler on 5/4/2024 in #help
WPF ContextMenu background color problems
No description
1 replies
CC#
Created by stigzler on 5/1/2024 in #help
WPF - how to incorporate existing code into a Content Control
Hi. I'm at the limits of my WPF-Fu. I have code monkeyed some xmal that can re-orientate two sides with a separator (think winforms spliterPanel) to be either horizontally or vertically aligned depending on a ViewModel property. The problem is now coming when I'm trying to fit my existing code into this lifted code. As I say - I'm out my depth as it's now getting into the realms of ContentControls and DataTemplates (nested I think at that). COuld someone please help out? Code re: the above here: https://paste.mod.gg/yveppqhunols/0
2 replies
CC#
Created by stigzler on 4/30/2024 in #help
Have a WPF treeview stretch to fit its container
No description
4 replies
CC#
Created by stigzler on 4/30/2024 in #help
WPF Treeview HierarchicalDataTemplate on Type with sub-collection
Hi Folks. I have the following two models (abridged for brevity) and mainViewModel:
internal class Gist
{
public ObservableCollection<GistFile> GistFiles { get; set; } = new();

public override string ToString()
{
return GistFiles.OrderBy(gf => gf.Filename).First().Filename;
}
}

internal class GistFile
{
public string Filename { get; set; }

public override string ToString()
{
return Filename;
}
}

internal class MainWindowViewModel : ViewModelBase
{
private ObservableCollection<Models.Gist> gists = new ObservableCollection<Models.Gist>();
public ObservableCollection<Models.Gist> Gists { get => gists; set => SetProperty(ref gists, value); }
}
internal class Gist
{
public ObservableCollection<GistFile> GistFiles { get; set; } = new();

public override string ToString()
{
return GistFiles.OrderBy(gf => gf.Filename).First().Filename;
}
}

internal class GistFile
{
public string Filename { get; set; }

public override string ToString()
{
return Filename;
}
}

internal class MainWindowViewModel : ViewModelBase
{
private ObservableCollection<Models.Gist> gists = new ObservableCollection<Models.Gist>();
public ObservableCollection<Models.Gist> Gists { get => gists; set => SetProperty(ref gists, value); }
}
and my xaml:
<TreeView x:Name="GistsTV" ItemsSource="{Binding Gists}" Width="200">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Gists.Files}">
<TextBlock Text="{Binding}"></TextBlock>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<TreeView x:Name="GistsTV" ItemsSource="{Binding Gists}" Width="200">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Gists.Files}">
<TextBlock Text="{Binding}"></TextBlock>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
I'm only getting the Gist displayed and not the GistFiles on a sub-level. I know I'm getting this wrapped around my head conceptually - what am I doing wrong?
5 replies
CC#
Created by stigzler on 4/28/2024 in #help
✅ WPF - how to approach a treeview displaying different types at different levels via MVVM
I'm just learning WPF - so seeking some design advice. I'm designing a Visual Studio extension to view and edit Gists. Each type Gist has a collection of type GistFiles (Files property in a Gist): I'll have 3 views inside the main View: GistsBrowser (user control=treeview), GistEditor (user control) and GistFileEditor (user control). I'm thinking I need to store the Collection of Gists in the MainViewModel, as different UserControls/ViewModels will need access to a shared GistsCollection to ensure synchronicity. For the GistsBrowser, I need to make a specific treeview control to display all Gists at the root level which can be expanded to show all of the GistFiles within a Gist. There will be no further child levels. Thus, this UserControl will need its own ViewModel (GistsBrowserViewModel). However, I'm stuck on a few things: How to structure the Gists collection in MainViewModel. Not sure if I need to map the Github data objects onto my own models, or map them directly onto a viewmodel. Do I need a Gist/GistFile viewmodel? Also, this links with: What data construct to use for the Gists collection and how to hook it up to a TreeView (given level 0 will be Gists and level 1 GistFiles) I hope that makes some kind of sense. Any pointers for further reading/research would be appreciated.
109 replies
CC#
Created by stigzler on 4/27/2024 in #help
✅ WPF - set UIElement (image) in XAML depending on databound Property Enum value
I am trying to dynamically change a StatusBar image via a view's XAML depending upon a databound ViewModel Property. The offender in question:
public StatusBarImage StatusBarImage { get => statusBarImage; set => SetProperty(ref statusBarImage, value); }
public StatusBarImage StatusBarImage { get => statusBarImage; set => SetProperty(ref statusBarImage, value); }
The enum:
internal enum StatusBarImage
{
Warning,
Error,
Information,
Success,
Fail
}
internal enum StatusBarImage
{
Warning,
Error,
Information,
Success,
Fail
}
Pseudocode I'm trying to achieve:
<StatusBar x:Name="MainSB" DockPanel.Dock="Bottom" Margin="4" Height="Auto">
<StackPanel Orientation="Horizontal">
Dear XAML, if {Binding StatusBarImage} == StatusBarImage.Warning
<imaging:CrispImage Width="16" Height="16" Moniker="{x:Static catalog:KnownMonikers.StatusWarning}" />
else if (if {Binding StatusBarImage} == StatusBarImage.Information)
<imaging:CrispImage Width="16" Height="16" Moniker="{x:Static catalog:KnownMonikers.StatusWarning}" />
<TextBlock Text="Operation Successful"></TextBlock>
</StackPanel>
</StatusBar>
<StatusBar x:Name="MainSB" DockPanel.Dock="Bottom" Margin="4" Height="Auto">
<StackPanel Orientation="Horizontal">
Dear XAML, if {Binding StatusBarImage} == StatusBarImage.Warning
<imaging:CrispImage Width="16" Height="16" Moniker="{x:Static catalog:KnownMonikers.StatusWarning}" />
else if (if {Binding StatusBarImage} == StatusBarImage.Information)
<imaging:CrispImage Width="16" Height="16" Moniker="{x:Static catalog:KnownMonikers.StatusWarning}" />
<TextBlock Text="Operation Successful"></TextBlock>
</StackPanel>
</StatusBar>
Could soemone give me a steer, please?
6 replies
CC#
Created by stigzler on 4/27/2024 in #help
✅ WPF - how to correctly call a method in the ViewModel when an event occurs in the View?
I want to call a method in the ViewModel when an event happens in the View. That is, when the View is loaded, I want it to execute a Authentication method in the ViewModel:
public partial class MainWindow : UserControl
{
internal MainWindowViewModel MainWindowVM;

private async Task MyToolWindow_LoadedAsync(object sender, RoutedEventArgs e)
{
await MainWindowVM.AuthenticateUserAsync();
}
}

internal class MainWindowViewModel: ViewModelBase
{
internal bool IsAuthenticated { get; set; } = false;

private GistClientService GistClientService = new GistClientService();

public MainWindowViewModel()
{
}

internal async Task AuthenticateUserAsync()
{
IsAuthenticated = await GistClientService.AuthenticateAsync();
}
}
public partial class MainWindow : UserControl
{
internal MainWindowViewModel MainWindowVM;

private async Task MyToolWindow_LoadedAsync(object sender, RoutedEventArgs e)
{
await MainWindowVM.AuthenticateUserAsync();
}
}

internal class MainWindowViewModel: ViewModelBase
{
internal bool IsAuthenticated { get; set; } = false;

private GistClientService GistClientService = new GistClientService();

public MainWindowViewModel()
{
}

internal async Task AuthenticateUserAsync()
{
IsAuthenticated = await GistClientService.AuthenticateAsync();
}
}
I get Error CS0407 'Task MainWindow.MyToolWindow_LoadedAsync(object, RoutedEventArgs)' has the wrong return type at compile time.When coding MyToolWindow_LoadedAsync, I followed the intellisence warning about naming, uing void and not awaiting to end up with how it looks preently. I know it's me - what am I doing wrong?
15 replies
CC#
Created by stigzler on 4/9/2024 in #help
WPF - Resource Dictionary - "the resource could not be resolved"
I'm working on a VS extension. This is a WPF UserControl, thus no app.xaml. I'm trying to move my styles out of the main control's body to seprarte resource dictionaries. I've followed guides, but getting an error on any elements whic previously worked when they were in <UserControl.Resources> In the user control:
<UserControl.Resources>

<converters:InverseBoolToVisibilityConverter x:Key="inverseBoolToVisibilityConverter" />
<converters:EmptyStringToVisibilityConverter x:Key="emptyStringToVisibilityConverter" />

<styleSelectors:TreeViewItemStyleSelector x:Key="treeViewItemStyleSelector" />

<ResourceDictionary x:Key="MainDictionary">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/GistManager;component/Styles/ToolbarStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

<CollectionViewSource x:Key="privateGistsSource"
Source="{Binding Gists}"
behaviors:CollectionViewSourceFilterBehavior.FilterCommand="{Binding PrivateFilterCommand}"
behaviors:CollectionViewSourceFilterBehavior.FilterTextBox="{Binding ElementName=searchBox}" />
</UserControl.Resources>
<UserControl.Resources>

<converters:InverseBoolToVisibilityConverter x:Key="inverseBoolToVisibilityConverter" />
<converters:EmptyStringToVisibilityConverter x:Key="emptyStringToVisibilityConverter" />

<styleSelectors:TreeViewItemStyleSelector x:Key="treeViewItemStyleSelector" />

<ResourceDictionary x:Key="MainDictionary">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/GistManager;component/Styles/ToolbarStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

<CollectionViewSource x:Key="privateGistsSource"
Source="{Binding Gists}"
behaviors:CollectionViewSourceFilterBehavior.FilterCommand="{Binding PrivateFilterCommand}"
behaviors:CollectionViewSourceFilterBehavior.FilterTextBox="{Binding ElementName=searchBox}" />
</UserControl.Resources>
and:
<Separator Style="{StaticResource toolBarSeparatorStyle}"/>
<Separator Style="{StaticResource toolBarSeparatorStyle}"/>
(this throws the error - working before removal). And the resource dict:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="toolBarSeparatorStyle" TargetType="Separator" >
<Setter Property="Background" Value="gray"></Setter>
<Setter Property="Margin" Value="4"></Setter>
</Style>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="toolBarSeparatorStyle" TargetType="Separator" >
<Setter Property="Background" Value="gray"></Setter>
<Setter Property="Margin" Value="4"></Setter>
</Style>
</ResourceDictionary>
What am I doing wrong?
2 replies
CC#
Created by stigzler on 4/7/2024 in #help
✅ Shorten Property code
Is there a simpler way to write this:
private bool _gistEdited = false;
public bool GistEdited
{
get {
return _gistEdited;
}
set
{
_gistEdited = value;
mainWindowControl.SaveButton.BorderBrush = new SolidColorBrush(Color.FromArgb(255,255,0,0));
}
}
private bool _gistEdited = false;
public bool GistEdited
{
get {
return _gistEdited;
}
set
{
_gistEdited = value;
mainWindowControl.SaveButton.BorderBrush = new SolidColorBrush(Color.FromArgb(255,255,0,0));
}
}
I know you can do:
// No private backing memeber
public int GistEdited { get; set; }
// No private backing memeber
public int GistEdited { get; set; }
And also:
// No private backing memeber
public bool GistEdited { get => GistEdited; set => mainWindowControl.SaveButton.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)); }
// No private backing memeber
public bool GistEdited { get => GistEdited; set => mainWindowControl.SaveButton.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)); }
But with the former, you can't do the mainWindowControl.SaveButton.BorderBrush =... and with the latter, you can't set GistEdited (as it'd result in an infinite loop)
6 replies
CC#
Created by stigzler on 4/4/2024 in #help
Casting types inline
I'm still not clear on all the routes to this, and which one is best for readability. I'm trying to get .Count from a child object of object sender. The Type hierarchy is:
DirectDragTreeViewItem.GistViewModel.GistModel.IList<GistFileModel>
DirectDragTreeViewItem.GistViewModel.GistModel.IList<GistFileModel>
Here are a couple of ways I've tried:
int directCastListCount = (GistViewModel)((DirectDragTreeViewItem)sender).DataContext) // wtf!? gave up here
int paternMatchedListCount = ((((sender as DirectDragTreeViewItem).DataContext as GistViewModel).Gist as GistModel).Files as IList<GistFileModel>).Count();
int directCastListCount = (GistViewModel)((DirectDragTreeViewItem)sender).DataContext) // wtf!? gave up here
int paternMatchedListCount = ((((sender as DirectDragTreeViewItem).DataContext as GistViewModel).Gist as GistModel).Files as IList<GistFileModel>).Count();
Of course things could be nested deeper. Which is the best way (realise 'best' is ambiguous)
8 replies
CC#
Created by stigzler on 4/3/2024 in #help
Inheritance best practice: test Type of subclass or use enum?
No description
4 replies
CC#
Created by stigzler on 4/2/2024 in #help
Visual Studio + WPF: "Could Not Load Assembly" despite dlls being available and reference correct
No description
19 replies
CC#
Created by stigzler on 3/31/2024 in #help
Refactor this if statement without an additional method
Sorry folks. I'm a bit codeblind atm. What's the neatest way to do the below without another Method (which would contain the repeated code). This feels like a code smell atm:
KryptonWorkspaceCell? cell = SpaceControl?.ActiveCell;
if (cell == null)
{
// ...create a new cell and place at the end of the root collection
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
}
else if (cell.Pages.Count() == 0)
{
// first remove the empty cell
SpaceControl!.Root.Children!.Remove(cell);
// now create a new cell and place at the end of the root collection
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
}
// cell is then used from hereon in
KryptonWorkspaceCell? cell = SpaceControl?.ActiveCell;
if (cell == null)
{
// ...create a new cell and place at the end of the root collection
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
}
else if (cell.Pages.Count() == 0)
{
// first remove the empty cell
SpaceControl!.Root.Children!.Remove(cell);
// now create a new cell and place at the end of the root collection
cell = new KryptonWorkspaceCell();
SpaceControl!.Root.Children!.Add(cell);
}
// cell is then used from hereon in
23 replies