Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Exmaple2.0/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@
<SimpleTheme></SimpleTheme>
<StyleInclude Source="avares://MsBox.Avalonia.Markdown/Controls/MarkdownView.axaml" />
</Application.Styles>

<Application.Resources>
<ResourceDictionary>
<!--This is only necessary if you want to override the default ViewLocator above-->
<local:EmbeddedViewLocator x:Key="EmbeddedMessageBoxViewLocator" />
</ResourceDictionary>
</Application.Resources>
</Application>
40 changes: 40 additions & 0 deletions Exmaple2.0/EmbeddedViewLocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Avalonia.Controls;
using Avalonia.Controls.Templates;

namespace Exmaple2._0;

public class EmbeddedViewLocator
: IDataTemplate
{
public static Control BuildView(object? data)
{
if (data is not null)
{
var name = data.GetType().AssemblyQualifiedName!.Replace("ViewModel", "View");
var type = Type.GetType(name);

if (type != null)
{
return (Control)Activator.CreateInstance(type)!;
}
else
{
return new TextBlock { Text = "Not Found: " + name };
}
}
else
{
return new TextBlock { Text = "Data object is null" };
}
}

public Control Build(object? data)
{
return BuildView(data);
}

public bool Match(object? data)
{
return data is not null;
}
}
6 changes: 6 additions & 0 deletions Exmaple2.0/ViewModels/EmbeddedViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Exmaple2._0.ViewModels;

public class EmbeddedViewModel : ViewModelBase
{
public string Message => "This is an embedded message!";
}
23 changes: 23 additions & 0 deletions Exmaple2.0/Views/EmbeddedView.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:Exmaple2._0.ViewModels"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Exmaple2._0.Views.EmbeddedView"
x:DataType="vm:EmbeddedViewModel">
<StackPanel>
<TextBlock Text="{Binding Message}"/>
<ComboBox>
<ComboBoxItem>
<TextBlock Text="Item 1"/>
</ComboBoxItem>
<ComboBoxItem>
<TextBlock Text="Item 2"/>
</ComboBoxItem>
<ComboBoxItem>
<TextBlock Text="Item 3"/>
</ComboBoxItem>
</ComboBox>
</StackPanel>
</UserControl>
11 changes: 11 additions & 0 deletions Exmaple2.0/Views/EmbeddedView.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Avalonia.Controls;

namespace Exmaple2._0.Views;

public partial class EmbeddedView : UserControl
{
public EmbeddedView()
{
InitializeComponent();
}
}
1 change: 1 addition & 0 deletions Exmaple2.0/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<ScrollViewer>
<StackPanel>
<Button Name="Standard_Show" Classes="View" Click="Standard_Show_OnClick" Content="Standard Show"/>
<Button Name="Standard_Context_Show" Classes="View" Click="Standard_Context_Show_OnClick" Content="Standard Context Show"/>
<Button Name="Standard_Dialog" Classes="View" Click="Standard_Dialog_OnClick" Content="Standard Dialog"/>
<Button Name="Standard_Popup" Classes="View" Click="Standard_Popup_OnClick" Content="Custom PopUp"/>
<Button Name="Custom_Show" Classes="View" Click="Custom_Dialog_OnClick" Content="Custom Show"/>
Expand Down
19 changes: 19 additions & 0 deletions Exmaple2.0/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;

using Exmaple2._0.ViewModels;

using MsBox.Avalonia;
using MsBox.Avalonia.Controls;
using MsBox.Avalonia.Dto;
using MsBox.Avalonia.Enums;
using MsBox.Avalonia.Models;
using MsBox.Avalonia.ViewModels;

using static System.Net.Mime.MediaTypeNames;

namespace Exmaple2._0.Views;

Expand All @@ -32,6 +39,18 @@ private async void Standard_Show_OnClick(object sender, RoutedEventArgs e)
var result = await box.ShowAsPopupAsync(this);
}

private async void Standard_Context_Show_OnClick(object sender, RoutedEventArgs e)
{
var box = MessageBoxManager
.GetMessageBoxStandard(
"Caption",
"Are you sure you would like to delete appender_replace_page_1?",
ButtonEnum.YesNo,
context: new EmbeddedViewModel());

var result = await box.ShowAsPopupAsync(this);
}

private async void Standard_Dialog_OnClick(object sender, RoutedEventArgs e)
{
int maxWidth = 500;
Expand Down
69 changes: 39 additions & 30 deletions MsBox.Avalonia/Controls/MsBoxStandardView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
d:DataContext="{x:Static viewModels:DesignDataContexts.StandardInputViewModel}"
x:Class="MsBox.Avalonia.Controls.MsBoxStandardView" MinWidth="{Binding MinWidth}"
x:DataType="viewModels:MsBoxStandardViewModel"
xmlns:local="clr-namespace:MsBox.Avalonia"
MaxWidth="{Binding MaxWidth}"
Width="{Binding Width}"
MinHeight="{Binding MinHeight}"
Expand All @@ -24,23 +25,23 @@
<KeyBinding Gesture="Ctrl+C" Command="{Binding Copy}" />
</UserControl.KeyBindings>
<UserControl.Styles>
<Style Selector="Image">
<Setter Property="Margin" Value="0,0,15,0" />
<Setter Property="MaxHeight" Value="50" />
<Setter Property="MaxWidth" Value="50" />
<Setter Property="VerticalAlignment" Value="Top" />
</Style>
<Style Selector="TextBox.styled">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="AcceptsReturn" Value="True" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="CaretBrush" Value="Transparent" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
</Style>
<Style Selector="TextBox.styled:pointerover /template/ Border#border">
<Style Selector="Image">
<Setter Property="Margin" Value="0,0,15,0" />
<Setter Property="MaxHeight" Value="50" />
<Setter Property="MaxWidth" Value="50" />
<Setter Property="VerticalAlignment" Value="Top" />
</Style>
<Style Selector="TextBox.styled">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="AcceptsReturn" Value="True" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="CaretBrush" Value="Transparent" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
</Style>
<Style Selector="TextBox.styled:pointerover /template/ Border#border">
<Setter Property="Background" Value="Transparent" />
</Style>
<Style Selector="TextBox.styled:pointerover /template/ Border#PART_BorderElement">
Expand All @@ -67,12 +68,12 @@
<Setter Property="MinWidth" Value="75" />
</Style>
<Style Selector="TextBlock.hyperlink">
<Setter Property="Foreground" Value="#3483eb" />
<Setter Property="Foreground" Value="#3483eb" />
</Style>
<Style Selector="TextBlock.hyperlink:pointerover">
<Setter Property="Cursor" Value="Hand" />
<Setter Property="TextDecorations" Value="Underline" />
<Setter Property="Foreground" Value="#3496eb" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="TextDecorations" Value="Underline" />
<Setter Property="Foreground" Value="#3496eb" />
</Style>
</UserControl.Styles>
<Grid Classes="MsBoxStandardContainer">
Expand All @@ -95,6 +96,7 @@
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!--Bold text-->
Expand All @@ -105,22 +107,29 @@
Text="{Binding ContentMessage}" IsVisible="{Binding !Markdown}" />
<controls:MarkdownView Grid.Row="1"
Markdown="{Binding ContentMessage}"
IsVisible="{Binding Markdown}" />
IsVisible="{Binding Markdown}" />

<!--Embedded View-->
<ContentControl Grid.Row="2"
Name="embeddedView"
Content="{Binding Path=Context, Mode=OneWay}"
IsVisible="{Binding IsContextVisible}"/>

<!--HyperLink-->
<TextBlock Grid.Row="3" IsVisible="{Binding IsHyperLinkVisible }" Name="HyperLink" Classes="hyperlink"
Text="{Binding HyperLinkText}"
attachadProperty:HyperLinkCommand.Command="{Binding HyperLinkCommand}" Margin="0,0,0,15">
</TextBlock>
</TextBlock>
</Grid>
<!--Input-->
<Grid Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" IsVisible="{Binding IsInputVisible}" Margin="0,0,0,15">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock FontFamily="{Binding FontFamily}" IsVisible="{Binding IsInputVisible}" Text="{Binding InputLabel}" VerticalAlignment="Center" Margin="0,0,15,0" />
<TextBox Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding InputValue, Mode=TwoWay}" FontFamily="{Binding FontFamily}" IsVisible="{Binding IsInputVisible}"
AcceptsReturn="{Binding IsInputMultiline}" />
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock FontFamily="{Binding FontFamily}" IsVisible="{Binding IsInputVisible}" Text="{Binding InputLabel}" VerticalAlignment="Center" Margin="0,0,15,0" />
<TextBox Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding InputValue, Mode=TwoWay}" FontFamily="{Binding FontFamily}" IsVisible="{Binding IsInputVisible}"
AcceptsReturn="{Binding IsInputMultiline}" />
</Grid>
<!--Buttons-->
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2"
Expand Down
11 changes: 11 additions & 0 deletions MsBox.Avalonia/Controls/MsBoxStandardView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Templates;

using MsBox.Avalonia.Base;
using MsBox.Avalonia.Enums;
Expand All @@ -14,6 +16,15 @@ public partial class MsBoxStandardView : UserControl, IFullApi<ButtonResult>, IS
public MsBoxStandardView()
{
InitializeComponent();

if (Application.Current.TryGetResource("EmbeddedMessageBoxViewLocator", out object? embeddedViewLocator))
{
var viewLocator = embeddedViewLocator as IDataTemplate;
if (viewLocator is not null)
{
embeddedView.DataTemplates.Add(viewLocator);
}
}
}

public void SetButtonResult(ButtonResult bdName)
Expand Down
5 changes: 5 additions & 0 deletions MsBox.Avalonia/Dto/MessageBoxStandardParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public class MessageBoxStandardParams : AbstractMessageBoxParams
/// </summary>
public Icon Icon { get; set; } = Icon.None;

/// <summary>
/// View model for embedded context.
/// </summary>
public object? Context { get; set; } = null;

/// <summary>
/// Default buttons
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion MsBox.Avalonia/MessageBoxManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,23 @@ public static IMsBox<ButtonResult> GetMessageBoxStandard(MessageBoxStandardParam
/// <param name="text"> Text of messagebox body </param>
/// <param name="enum"> Buttons of messagebox (default OK) </param>
/// <param name="icon"> Icon of messagebox (default no icon) </param>
/// <param name="context"> Embedded view model in messagebox (default null) </param>
/// <param name="windowStartupLocation"> Startup location of messagebox (default center screen) </param>
/// <param name="style"></param>
/// <returns></returns>
/// <remarks>
/// Recommended method for message box
/// </remarks>
public static IMsBox<ButtonResult> GetMessageBoxStandard(string title, string text,
ButtonEnum @enum = ButtonEnum.Ok, Icon icon = Icon.None,
ButtonEnum @enum = ButtonEnum.Ok, Icon icon = Icon.None, object? context = null,
WindowStartupLocation windowStartupLocation = WindowStartupLocation.CenterScreen) =>
GetMessageBoxStandard(new MessageBoxStandardParams
{
ContentTitle = title,
ContentMessage = text,
ButtonDefinitions = @enum,
Icon = icon,
Context = context,
WindowStartupLocation = windowStartupLocation
});
}
9 changes: 9 additions & 0 deletions MsBox.Avalonia/ViewModels/MsBoxStandardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public MsBoxStandardViewModel(MessageBoxStandardParams @params) :
_enterDefaultButton = @params.EnterDefaultButton;
_escDefaultButton = @params.EscDefaultButton;

Context = @params.Context;
if (Context is not null)
{
IsContextVisible = true;
}

SetButtons(@params.ButtonDefinitions);
ButtonClickCommand = new RelayCommand(o => ButtonClick(o.ToString()));
EnterClickCommand = new RelayCommand(_ => EnterClick());
Expand Down Expand Up @@ -48,6 +54,9 @@ public void SetFullApi(IFullApi<ButtonResult> fullApi)
public override string InputValue { get; set; }
public override bool IsInputMultiline { get; internal set; }
public override bool IsInputVisible { get; internal set; }
public virtual object? Context { get; internal set; } = null;
public virtual bool IsContextVisible { get; internal set; } = false;

#endregion

public RelayCommand ButtonClickCommand { get; }
Expand Down