Skip to content

Instantly share code, notes, and snippets.

@nexpr
Created November 6, 2016 15:09
Show Gist options
  • Select an option

  • Save nexpr/c8691e2dccafe83ca647b6d68a90b64f to your computer and use it in GitHub Desktop.

Select an option

Save nexpr/c8691e2dccafe83ca647b6d68a90b64f to your computer and use it in GitHub Desktop.
ボタンを自作できる MessageBox
private void button_Click_1(object sender, RoutedEventArgs e)
{
var selection = new Dialog().show("うぃんどうたいとる", "ああああああい\nいいいいいいいいい\nうううううううううううう\nnagaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaai-text\n123", new List<string>
{
"ボタン01", "ボタン02", "ボタン03",
"ボタン04", "ポタン05", "ボタン06",
"ボタン07", "ボタン08", "ボタン09",
"ボタン10", "ボタン11", "ボタン12",
});
Console.WriteLine(selection);
}
<Window x:Class="liblib.Dialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:liblib"
mc:Ignorable="d"
ResizeMode="NoResize"
SizeToContent="WidthAndHeight"
Title="{Binding title}"
FontSize="13"
MaxWidth="640"
MaxHeight="640"
d:DesignHeight="200" d:DesignWidth="200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding message}" Margin="30 40 30 30"/>
<Border Grid.Row="1" Background="Cornsilk">
<ItemsControl HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{Binding buttons}" Margin="20 5" Button.Click="onbuttonclick" AlternationCount="999">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Margin="10" Padding="20 4" Background="#E0E0E0" Content="{Binding}" Tag="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=(ItemsControl.AlternationIndex)}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace liblib
{
/// <summary>
/// Dialog.xaml の相互作用ロジック
/// </summary>
public partial class Dialog : Window
{
private class BindingData : NotifyHelper
{
private string _title = "";
public string title
{
get { return _title; }
set
{
this._title = value;
this.notifyPropertyChange(nameof(title));
}
}
private string _message = "";
public string message
{
get { return _message; }
set
{
this._message = value;
this.notifyPropertyChange(nameof(message));
}
}
private List<string> _buttons = new List<string>();
public List<string> buttons
{
get { return _buttons; }
set
{
this._buttons = value;
this.notifyPropertyChange(nameof(buttons));
}
}
}
private int select { get; set; } = -1;
public Dialog()
{
InitializeComponent();
}
public int show(string title, string message, IEnumerable<string> selections)
{
var buttons = selections.ToList();
if(buttons.Count > 16)
{
throw new ArgumentOutOfRangeException("Too many buttons.");
}
this.DataContext = new BindingData
{
title = title,
message = message,
buttons = selections.ToList(),
};
base.ShowDialog();
return this.select;
}
private void onbuttonclick(object sender, RoutedEventArgs e)
{
var button = (Button)e.OriginalSource;
var index = (int)button.Tag;
this.select = index;
this.Close();
}
public new void Show()
{
throw new NotSupportedException("Please use 'show' method.");
}
public new void ShowDialog()
{
throw new NotSupportedException("Please use 'show' method.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment