mirror of
https://gitcode.com/gh_mirrors/se/Semi.Avalonia
synced 2026-03-02 15:50:49 +08:00
feat: enhance display value & add Copy button.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:vm="clr-namespace:Semi.Avalonia.Demo.ViewModels"
|
||||
xmlns:pages="clr-namespace:Semi.Avalonia.Demo.Pages"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="Semi.Avalonia.Demo.Pages.VariablesDemo"
|
||||
x:DataType="vm:VariablesDemoViewModel">
|
||||
@@ -33,10 +34,18 @@
|
||||
<DataGridTemplateColumn Width="*" Header="ResourceKey">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate DataType="vm:VariableItemViewModel">
|
||||
<SelectableTextBlock
|
||||
Margin="12,0,12,0"
|
||||
VerticalAlignment="Center"
|
||||
Text="{Binding ResourceKey}" />
|
||||
<Panel>
|
||||
<SelectableTextBlock
|
||||
Margin="12,0"
|
||||
VerticalAlignment="Center"
|
||||
Text="{Binding ResourceKey}" />
|
||||
<Button
|
||||
HorizontalAlignment="Right"
|
||||
Command="{Binding $parent[pages:VariablesDemo].Copy}"
|
||||
CommandParameter="{Binding ResourceKey}"
|
||||
Theme="{DynamicResource IconBorderlessButton}"
|
||||
Content="{StaticResource SemiIconCopy}" />
|
||||
</Panel>
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
@@ -50,10 +59,20 @@
|
||||
<DataGridTemplateColumn Width="300" Header="Value">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate DataType="vm:VariableItemViewModel">
|
||||
<SelectableTextBlock
|
||||
Margin="12,0,12,0"
|
||||
VerticalAlignment="Center"
|
||||
Text="{Binding Value}" />
|
||||
<Panel>
|
||||
<SelectableTextBlock
|
||||
Margin="12,0"
|
||||
HorizontalAlignment="Left"
|
||||
VerticalAlignment="Center"
|
||||
Text="{Binding Value}"
|
||||
TextWrapping="Wrap" />
|
||||
<Button
|
||||
HorizontalAlignment="Right"
|
||||
Command="{Binding $parent[pages:VariablesDemo].Copy}"
|
||||
CommandParameter="{Binding Value}"
|
||||
Theme="{DynamicResource IconBorderlessButton}"
|
||||
Content="{StaticResource SemiIconCopy}" />
|
||||
</Panel>
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Avalonia.Controls;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Threading;
|
||||
using Semi.Avalonia.Demo.ViewModels;
|
||||
@@ -19,4 +20,14 @@ public partial class VariablesDemo : UserControl
|
||||
var vm = this.DataContext as VariablesDemoViewModel;
|
||||
await Dispatcher.UIThread.InvokeAsync(() => { vm?.InitializeResources(); });
|
||||
}
|
||||
|
||||
public async Task Copy(object? o)
|
||||
{
|
||||
if (o is null) return;
|
||||
var toplevel = TopLevel.GetTopLevel(this);
|
||||
if (toplevel?.Clipboard is { } c)
|
||||
{
|
||||
await c.SetTextAsync(o.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Media;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Semi.Avalonia.Demo.Constant;
|
||||
using Semi.Avalonia.Tokens;
|
||||
@@ -46,10 +49,25 @@ public partial class VariableGridViewModel : ObservableObject
|
||||
{
|
||||
if (dictionary?.TryGetValue(key, out var value) ?? false)
|
||||
{
|
||||
VariableItems.Add(new VariableItemViewModel(name, value ?? string.Empty, key));
|
||||
VariableItems.Add(new VariableItemViewModel(name, GetValueString(value), key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetValueString(object? value)
|
||||
{
|
||||
if (value is null) return string.Empty;
|
||||
|
||||
return value switch
|
||||
{
|
||||
double d => d.ToString(CultureInfo.InvariantCulture),
|
||||
CornerRadius c => c.IsUniform ? $"{c.TopLeft}" : c.ToString(),
|
||||
Thickness t => t.IsUniform ? $"{t.Left}" : t.ToString(),
|
||||
FontWeight fontWeight => Convert.ToInt32(fontWeight).ToString(),
|
||||
FontFamily fontFamily => fontFamily.FamilyNames.ToString(),
|
||||
_ => value.ToString()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public partial class VariableItemViewModel : ObservableObject
|
||||
|
||||
Reference in New Issue
Block a user