Date: 08.06.2017 2:56:56
Date: 08.06.2017 7:53:16
Date: 08.06.2017 19:12:15
Если формы для разных типов принципиально различаются, то да, лепить для каждого свою форму. Если они выглядят одинаково, можно справиться с помощью привязок.
Вам нужно создать класс, который будет содержать параметры сообщения - текст, тип, необходимые кнопки и т.п. - который будет передаваться в конструктор окна. Поскольку вид окна сообщения не меняется во времени, он может быть предельно простым, без NotifyPropertyChanged. Визуальные параметры окна связать со свойствами этого класса с помощью DataTrigger. А для переменного числа кнопок использовать ItemsControl.ItemTemplate
public enum MessageBoxType
{
Error,Warning,Information
}
public class MessageBoxParams
{
public string Text { get; set; }
public MessageBoxType Type { get; set; }
public string[] Buttons { get; set; }
public MessageBoxParams(string text,MessageBoxType type,string[] butt)
{
Text = text; Type = type; Buttons = butt;
}
public MessageBoxParams()
{
Text = ""; Type = MessageBoxType.Information; Buttons = new string[] { "OK" };
}
}
Разметка окна:
<Window x:Class="WpfTestApp1.CoolMessageBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="399">
<Window.Style>
<Style TargetType="{x:Type Window}">
<Setter Property="Title" Value="Предупреждение"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="Error">
<Setter Property="Title" Value="Ошибка"/>
</DataTrigger>
<DataTrigger Binding="{Binding Type}" Value="Information">
<Setter Property="Title" Value="Информация"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Style>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="70"/>
</Grid.RowDefinitions>
<TextBlock HorizontalAlignment="Stretch" Grid.Row="0" TextAlignment="Center"
Name="textBlock1" Text="{Binding Path=Text}" VerticalAlignment="Stretch" >
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="Error">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding Type}" Value="Information">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style></TextBlock>
<ItemsControl Name="items" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel VerticalAlignment="Center" HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Path=''}" Click="button1_Click"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
Код окна:
public partial class CoolMessageBox : Window
{
MessageBoxParams pars;
public string Result { get; set; }
public CoolMessageBox(MessageBoxParams p)
{
InitializeComponent();
pars = p;
this.DataContext = pars;
items.ItemsSource = pars.Buttons;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Result = (sender as Button).Content.ToString();
this.Close();
}
}
Использование:
CoolMessageBox box = new CoolMessageBox(
new MessageBoxParams("Ваше любимое животное?",
MessageBoxType.Information,
new string[]{"Собака","Кошка"}));
box.ShowDialog();
label1.Content = "Пользователь выбрал: "+box.Result;
Если среда не позволяет плодить окна - вместо Window использовать Popup + UserControl, но в остальном все также.
Автор: VadimTagil