Bind SelectedItem instead of SelecteIndex

This commit is contained in:
Stone_Red
2024-03-30 13:57:21 +01:00
parent b886c6e91c
commit a74d1285ad
2 changed files with 14 additions and 13 deletions
+13 -12
View File
@@ -30,26 +30,26 @@ public partial class MainWindowViewModel : ViewModelBase
private readonly StringBuilder html = new(); private readonly StringBuilder html = new();
private StringBuilder? content; private StringBuilder? content;
private int currentTabIndex; private TabItem currentTab = null!;
private bool showSidePanel; private bool showSidePanel;
private bool isBusy; private bool isBusy;
private int progress; private int progress;
public ObservableCollection<TabItem> Tabs => GlobalState.Tabs; public ObservableCollection<TabItem> Tabs => GlobalState.Tabs;
public string Title => $"{nameof(Markdowser)} - {Assembly.GetExecutingAssembly().GetName().Version?.ToString()}"; public string Title => $"{nameof(Markdowser)} - {Assembly.GetExecutingAssembly().GetName().Version?.ToString()}";
public int CurrentTabIndex public TabItem CurrentTab
{ {
get => currentTabIndex; get => currentTab;
set set
{ {
if (CurrentTab is not null) if (currentTab is not null)
{ {
CurrentTab.Tag = Url; currentTab.Tag = Url;
} }
_ = this.RaiseAndSetIfChanged(ref currentTabIndex, value); _ = this.RaiseAndSetIfChanged(ref currentTab!, value);
Url = CurrentTab?.Tag?.ToString() ?? string.Empty; Url = currentTab?.Tag?.ToString() ?? string.Empty;
FetchUrl(); FetchUrl();
} }
@@ -128,9 +128,12 @@ public partial class MainWindowViewModel : ViewModelBase
public ICommand CloseTab => ReactiveCommand.Create(() => public ICommand CloseTab => ReactiveCommand.Create(() =>
{ {
if (Tabs.Count > 1 && Tabs.Remove(CurrentTab)) if (Tabs.Count > 1)
{ {
CurrentTabIndex = Math.Max(0, CurrentTabIndex - 1); int currentIndex = Tabs.IndexOf(CurrentTab);
_ = Tabs.Remove(CurrentTab);
CurrentTab = currentIndex > 0 ? Tabs[currentIndex - 1] : Tabs[0];
this.RaisePropertyChanged(nameof(CloseTabEnabled)); this.RaisePropertyChanged(nameof(CloseTabEnabled));
} }
}); });
@@ -139,12 +142,10 @@ public partial class MainWindowViewModel : ViewModelBase
{ {
TabItem tab = new() { Header = "New Tab", Name = Guid.NewGuid().ToString() }; TabItem tab = new() { Header = "New Tab", Name = Guid.NewGuid().ToString() };
Tabs.Add(tab); Tabs.Add(tab);
CurrentTabIndex = Tabs.Count - 1; CurrentTab = tab;
this.RaisePropertyChanged(nameof(CloseTabEnabled)); this.RaisePropertyChanged(nameof(CloseTabEnabled));
}); });
private TabItem CurrentTab => Tabs[CurrentTabIndex];
private StringBuilder DefaultContent => new StringBuilder() private StringBuilder DefaultContent => new StringBuilder()
.AppendLine($"![Logo](avares://Markdowser/Assets/Markdowser-{(Settings.Current.DarkMode ? "Dark" : "Light")}-Transparent.png)") .AppendLine($"![Logo](avares://Markdowser/Assets/Markdowser-{(Settings.Current.DarkMode ? "Dark" : "Light")}-Transparent.png)")
.AppendLine() .AppendLine()
+1 -1
View File
@@ -35,7 +35,7 @@
<Grid RowDefinitions="Auto, *, Auto"> <Grid RowDefinitions="Auto, *, Auto">
<Grid Grid.Row="0" ColumnDefinitions="*, Auto, Auto"> <Grid Grid.Row="0" ColumnDefinitions="*, Auto, Auto">
<TabControl Grid.Column="0" Theme="{DynamicResource ScrollTabControl}" SelectedIndex="{Binding CurrentTabIndex, Mode=TwoWay}" ItemsSource="{Binding Tabs}" /> <TabControl Grid.Column="0" Theme="{DynamicResource ScrollTabControl}" SelectedItem="{Binding CurrentTab, Mode=TwoWay}" ItemsSource="{Binding Tabs}" />
<Button Grid.Column="1" Command="{Binding CloseTab}" IsEnabled="{Binding CloseTabEnabled}" HorizontalAlignment="Right" VerticalAlignment="Top" Height="32" i:Attached.Icon="fa-solid fa-xmark" /> <Button Grid.Column="1" Command="{Binding CloseTab}" IsEnabled="{Binding CloseTabEnabled}" HorizontalAlignment="Right" VerticalAlignment="Top" Height="32" i:Attached.Icon="fa-solid fa-xmark" />
<Button Grid.Column="2" Command="{Binding NewTab}" HorizontalAlignment="Right" VerticalAlignment="Top" Height="32" i:Attached.Icon="fa-solid fa-plus" /> <Button Grid.Column="2" Command="{Binding NewTab}" HorizontalAlignment="Right" VerticalAlignment="Top" Height="32" i:Attached.Icon="fa-solid fa-plus" />
</Grid> </Grid>