fix:should be PathPickerForListView in demo

add:Maybe we can add a command demo here to show how to trigger the bound command.
change:I think these two themes should be merged, and the "AcceptReturn" should just be controlled by "AllowMultiple"
add:One more thing, we should add a property to control what to do when user cancelled a selection. Sometimes I just want to keep the original selection.
This commit is contained in:
望尘空忧
2025-01-13 19:08:55 +08:00
parent c6bbcf7226
commit d3aa632d1d
4 changed files with 54 additions and 39 deletions

View File

@@ -32,6 +32,10 @@
<ToggleButton Name="AllowMultiple" Content="AllowMultiple" u:FormItem.NoLabel="True" <ToggleButton Name="AllowMultiple" Content="AllowMultiple" u:FormItem.NoLabel="True"
HorizontalAlignment="Stretch"> HorizontalAlignment="Stretch">
</ToggleButton> </ToggleButton>
<ToggleButton Name="IsCancelingPickerAlsoTriggers" Content="Canceling selection also triggers the Command."
u:FormItem.NoLabel="True"
HorizontalAlignment="Stretch">
</ToggleButton>
<u:EnumSelector Name="UsePickerType" EnumType="u:UsePickerTypes" u:FormItem.Label="UsePickerType"></u:EnumSelector> <u:EnumSelector Name="UsePickerType" EnumType="u:UsePickerTypes" u:FormItem.Label="UsePickerType"></u:EnumSelector>
</u:Form> </u:Form>
</StackPanel> </StackPanel>
@@ -47,20 +51,9 @@
AllowMultiple="{Binding #AllowMultiple.IsChecked}" AllowMultiple="{Binding #AllowMultiple.IsChecked}"
UsePickerType="{Binding #UsePickerType.Value}" UsePickerType="{Binding #UsePickerType.Value}"
SelectedPathsText="{Binding Path,Mode=OneWayToSource}" SelectedPathsText="{Binding Path,Mode=OneWayToSource}"
SelectedPaths="{Binding Paths,Mode=OneWayToSource}"> SelectedPaths="{Binding Paths,Mode=OneWayToSource}"
</u:PathPicker> Command="{Binding SelectedCommand}"
</HeaderedContentControl> IsCancelingPickerAlsoTriggers="{Binding #IsCancelingPickerAlsoTriggers.IsChecked}">
<HeaderedContentControl Header="PathPickerForMultipleText">
<u:PathPicker Title="{Binding #Title.Text}"
Theme="{DynamicResource PathPickerForMultipleText}"
SuggestedFileName="{Binding #SuggestedFileName.Text}"
SuggestedStartPath="{Binding #SuggestedStartPath.Text}"
FileFilter="{Binding #FileFilter.Text}"
DefaultFileExtension="{Binding #DefaultFileExtension.Text}"
AllowMultiple="{Binding #AllowMultiple.IsChecked}"
UsePickerType="{Binding #UsePickerType.Value}"
SelectedPathsText="{Binding Path,Mode=OneWayToSource}"
SelectedPaths="{Binding Paths,Mode=OneWayToSource}">
</u:PathPicker> </u:PathPicker>
</HeaderedContentControl> </HeaderedContentControl>
<HeaderedContentControl Header="PathPickerOnlyButton"> <HeaderedContentControl Header="PathPickerOnlyButton">
@@ -73,11 +66,13 @@
AllowMultiple="{Binding #AllowMultiple.IsChecked}" AllowMultiple="{Binding #AllowMultiple.IsChecked}"
UsePickerType="{Binding #UsePickerType.Value}" UsePickerType="{Binding #UsePickerType.Value}"
SelectedPathsText="{Binding Path,Mode=OneWayToSource}" SelectedPathsText="{Binding Path,Mode=OneWayToSource}"
SelectedPaths="{Binding Paths,Mode=OneWayToSource}"> SelectedPaths="{Binding Paths,Mode=OneWayToSource}"
Command="{Binding SelectedCommand}"
IsCancelingPickerAlsoTriggers="{Binding #IsCancelingPickerAlsoTriggers.IsChecked}">
</u:PathPicker> </u:PathPicker>
</HeaderedContentControl> </HeaderedContentControl>
<HeaderedContentControl Header="PathPickerForListView"> <HeaderedContentControl Header="PathPickerForListView">
<u:PathPicker Theme="{DynamicResource PathPickerForList}" <u:PathPicker Theme="{DynamicResource PathPickerForListView}"
Title="{Binding #Title.Text}" Title="{Binding #Title.Text}"
SuggestedFileName="{Binding #SuggestedFileName.Text}" SuggestedFileName="{Binding #SuggestedFileName.Text}"
SuggestedStartPath="{Binding #SuggestedStartPath.Text}" SuggestedStartPath="{Binding #SuggestedStartPath.Text}"
@@ -86,13 +81,16 @@
AllowMultiple="{Binding #AllowMultiple.IsChecked}" AllowMultiple="{Binding #AllowMultiple.IsChecked}"
UsePickerType="{Binding #UsePickerType.Value}" UsePickerType="{Binding #UsePickerType.Value}"
SelectedPathsText="{Binding Path,Mode=OneWayToSource}" SelectedPathsText="{Binding Path,Mode=OneWayToSource}"
SelectedPaths="{Binding Paths,Mode=OneWayToSource}"> SelectedPaths="{Binding Paths,Mode=OneWayToSource}"
Command="{Binding SelectedCommand}"
IsCancelingPickerAlsoTriggers="{Binding #IsCancelingPickerAlsoTriggers.IsChecked}">
</u:PathPicker> </u:PathPicker>
</HeaderedContentControl> </HeaderedContentControl>
</StackPanel> </StackPanel>
</ScrollViewer> </ScrollViewer>
<ScrollViewer Grid.Column="1" Grid.Row="0" Grid.RowSpan="2"> <ScrollViewer Grid.Column="1" Grid.Row="0" Grid.RowSpan="2">
<StackPanel Spacing="1"> <StackPanel Spacing="1">
<TextBlock Text="{Binding CommandTriggerCount,StringFormat='Command Trigger Count:{0}'}"></TextBlock>
<HeaderedContentControl Header="SelectedPathsText"> <HeaderedContentControl Header="SelectedPathsText">
<TextBox Name="SelectedPath" u:FormItem.Label="SelectedPath" IsReadOnly="True" <TextBox Name="SelectedPath" u:FormItem.Label="SelectedPath" IsReadOnly="True"
Text="{Binding Path}"> Text="{Binding Path}">

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace Ursa.Demo.ViewModels; namespace Ursa.Demo.ViewModels;
@@ -7,4 +8,11 @@ public partial class PathPickerDemoViewModel : ViewModelBase
{ {
[ObservableProperty] private string? _path; [ObservableProperty] private string? _path;
[ObservableProperty] private IReadOnlyList<string>? _paths; [ObservableProperty] private IReadOnlyList<string>? _paths;
[ObservableProperty] private int _commandTriggerCount = 0;
[RelayCommand]
private void Selected(IReadOnlyList<string> paths)
{
CommandTriggerCount++;
}
} }

View File

@@ -11,32 +11,31 @@
Content="{TemplateBinding Title}" Content="{TemplateBinding Title}"
Margin="1,0,0,0"> Margin="1,0,0,0">
</Button> </Button>
<TextBox DockPanel.Dock="Left" <TextBox Name="PART_TextBox"
DockPanel.Dock="Left"
AcceptsReturn="{TemplateBinding AllowMultiple}"
Text="{TemplateBinding SelectedPathsText,Mode=TwoWay}"> Text="{TemplateBinding SelectedPathsText,Mode=TwoWay}">
</TextBox> </TextBox>
</DockPanel> </DockPanel>
</ControlTemplate> </ControlTemplate>
</Setter> </Setter>
</ControlTheme>
<ControlTheme x:Key="PathPickerForMultipleText" TargetType="ursa:PathPicker"> <Style Selector="^[AllowMultiple=False]">
<Setter Property="Template"> <Style Selector="^ /template/ Button#PART_Button">
<ControlTemplate> <Setter Property="DockPanel.Dock" Value="Right"></Setter>
<DockPanel HorizontalAlignment="{TemplateBinding HorizontalAlignment}" </Style>
VerticalAlignment="{TemplateBinding VerticalAlignment}"> <Style Selector="^ /template/ TextBox#PART_TextBox">
<Button Name="PART_Button" <Setter Property="DockPanel.Dock" Value="Left"></Setter>
DockPanel.Dock="Top" </Style>
HorizontalAlignment="Stretch" </Style>
Content="{TemplateBinding Title}" <Style Selector="^[AllowMultiple=True]">
Margin="0,0,0,1"> <Style Selector="^ /template/ Button#PART_Button">
</Button> <Setter Property="DockPanel.Dock" Value="Top"></Setter>
<TextBox DockPanel.Dock="Bottom" </Style>
Text="{TemplateBinding SelectedPathsText,Mode=TwoWay}" <Style Selector="^ /template/ TextBox#PART_TextBox">
AcceptsReturn="True"> <Setter Property="DockPanel.Dock" Value="Bottom"></Setter>
</TextBox> </Style>
</DockPanel> </Style>
</ControlTemplate>
</Setter>
</ControlTheme> </ControlTheme>

View File

@@ -54,6 +54,16 @@ public class PathPicker : TemplatedControl
AvaloniaProperty.Register<PathPicker, string?>( AvaloniaProperty.Register<PathPicker, string?>(
nameof(SelectedPathsText), defaultBindingMode: BindingMode.TwoWay); nameof(SelectedPathsText), defaultBindingMode: BindingMode.TwoWay);
public static readonly StyledProperty<bool> IsCancelingPickerAlsoTriggersProperty =
AvaloniaProperty.Register<PathPicker, bool>(
nameof(IsCancelingPickerAlsoTriggers));
public bool IsCancelingPickerAlsoTriggers
{
get => GetValue(IsCancelingPickerAlsoTriggersProperty);
set => SetValue(IsCancelingPickerAlsoTriggersProperty, value);
}
public string? SelectedPathsText public string? SelectedPathsText
{ {
get => GetValue(SelectedPathsTextProperty); get => GetValue(SelectedPathsTextProperty);
@@ -129,7 +139,7 @@ public class PathPicker : TemplatedControl
{ {
_twoConvertLock = true; _twoConvertLock = true;
var stringBuilder = new StringBuilder(); var stringBuilder = new StringBuilder();
stringBuilder.Append(SelectedPaths.FirstOrDefault()); stringBuilder.Append(SelectedPaths[0]);
foreach (var item in SelectedPaths.Skip(1)) foreach (var item in SelectedPaths.Skip(1))
{ {
stringBuilder.AppendLine(item); stringBuilder.AppendLine(item);
@@ -268,7 +278,7 @@ public class PathPicker : TemplatedControl
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
if (SelectedPaths.Count != 0) if (SelectedPaths.Count != 0 || IsCancelingPickerAlsoTriggers)
Command?.Execute(SelectedPaths); Command?.Execute(SelectedPaths);
} }
catch (Exception exception) catch (Exception exception)