C# 动态创建窗口时,按钮与中心对齐
我在运行时动态创建一个窗口。我有定制的按钮。我需要在运行时根据请求相应地获得“是,否”,“确定,取消”,“确定”,“是,否,取消”。我这里有图片。 这张图片描述了当按钮的水平对齐正确时,我并没有将两个按钮都向右对齐。你能帮我解决这个问题吗。我也在下面分享了我的代码。问题是按钮集合正在将行划分为我们需要的按钮数。 我的密码C# 动态创建窗口时,按钮与中心对齐,c#,wpf,xaml,user-interface,wpf-controls,C#,Wpf,Xaml,User Interface,Wpf Controls,我在运行时动态创建一个窗口。我有定制的按钮。我需要在运行时根据请求相应地获得“是,否”,“确定,取消”,“确定”,“是,否,取消”。我这里有图片。 这张图片描述了当按钮的水平对齐正确时,我并没有将两个按钮都向右对齐。你能帮我解决这个问题吗。我也在下面分享了我的代码。问题是按钮集合正在将行划分为我们需要的按钮数。 我的密码 internal sealed class MessageBoxModule : Window { private static Style _ctrlButtonS
internal sealed class MessageBoxModule : Window
{
private static Style _ctrlButtonStyle;
public new static readonly DependencyProperty TitleProperty;
public static readonly DependencyProperty MessageProperty;
public static readonly DependencyProperty ButtonCollectionProperty =
DependencyProperty.Register("ButtonCollection", typeof(IList<Button>), typeof(MessageBoxModule), new PropertyMetadata(new List<Button>()));
public MessageBoxModule()
{
WindowStartupLocation = WindowStartupLocation.CenterScreen;
AllowsTransparency = true;WindowStyle = WindowStyle.None;
//...
}
}
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption)
{
var mbox = new MessageBoxModule();
boxes.Add(mbox); mbox.Message = messageBoxText; mbox.Title = caption;
switch (button)
{
case MessageBoxButton.OKCancel:
mbox.ButtonCollection.Add(CreateButton(mbox, "OK"));
mbox.ButtonCollection.Add(CreateButton(mbox, "Cancel"));
break;
//.... And so on.
}
var result = mbox.ShowDialog();
switch (button)
{
case MessageBoxButton.YesNoCancel://and so on.
}
}
private static Button CreateButton(string content, bool isCancel, RoutedEventHandler clickHandler)
{
Button btn = new Button();
btn.Padding = new Thickness(20, 3, 20, 3);
btn.Content = content;
btn.Style = _ctrlButtonStyle;//Custom button Style in WPF
btn.Click += clickHandler;
btn.Height = 25; btn.Width = 75;
return btn;
}
内部密封类MessageBoxModule:窗口
{
私有静态样式_ctrlButtonStyle;
公共新静态只读从属属性TitleProperty;
公共静态只读DependencyProperty MessageProperty;
公共静态只读DependencyProperty按钮CollectionProperty=
Register(“ButtonCollection”、typeof(IList)、typeof(MessageBoxModule)、new PropertyMetadata(new List());
public MessageBoxModule()
{
WindowsStartUplocation=WindowsStartUplocation.CenterScreen;
AllowTransparency=true;WindowsStyle=WindowsStyle.None;
//...
}
}
公共静态MessageBoxResult显示(窗口所有者、字符串messageBoxText、字符串标题)
{
var mbox=新MessageBoxModule();
box.Add(mbox);mbox.Message=messageBoxText;mbox.Title=caption;
开关(按钮)
{
case MessageBoxButton.ok取消:
添加(CreateButton(mbox,“OK”);
添加(CreateButton(mbox,“取消”);
打破
//……等等。
}
var result=mbox.ShowDialog();
开关(按钮)
{
case MessageBoxButton.YesNoCancel://and 等等。
}
}
私有静态按钮CreateButton(字符串内容、bool isCancel、RoutedEventHandler clickHandler)
{
按钮btn=新按钮();
填料=新厚度(20,3,20,3);
btn.Content=内容;
btn.Style=\u ctrlButtonStyle;//WPF中的自定义按钮样式
btn.Click+=clickHandler;
btn.高度=25;btn.宽度=75;
返回btn;
}
MessageBox样式:此处,垂直和水平对齐设置为居中。即使我把它改成了右边,我也遇到了像图中那样的问题
<ControlTemplate x:Key="MessageBoxCt"
TargetType="{x:Type Helper:MessageBoxModule}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="3" Margin="8">
<Grid x:Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBlock x:Name="textBlock" Text="{TemplateBinding Title}"/>
<Border Grid.Row="0" BorderThickness="0,0,0,1"/>
<TextBlock Text="{TemplateBinding Message}"Grid.Row="1"Margin="10"TextTrimming="None"Foreground="{TemplateBinding Foreground}"
TextWrapping="WrapWithOverflow"FontSize="{TemplateBinding FontSize}" />
<ItemsControl Grid.Row="2"Margin="10"ItemsSource="{TemplateBinding ButtonCollection}"
ScrollViewer.VerticalScrollBarVisibility= "Disabled"HorizontalContentAlignment="Right"VerticalContentAlignment="Center"Padding="0,0,5,0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Border>
</ControlTemplate>
使用
UniformGrid
行中的每个项目具有相同的宽度,并且使用HorizontalContentAlignment=“Right”
中的按钮将右对齐。请尝试右对齐整个项目控件
:
<ItemsControl Grid.Row="2"
...
HorizontalAlignment="Right">
...
</ItemsControl>
...
使用UniformGrid
行中的每个项目具有相同的宽度,并且使用HorizontalContentAlignment=“Right”
中的按钮将右对齐。请尝试右对齐整个项目控件
:
<ItemsControl Grid.Row="2"
...
HorizontalAlignment="Right">
...
</ItemsControl>
...
没有你的代码的人很难猜到这里到底发生了什么。我猜您的行定义不正确。。。为什么要将一个设置为“*”
?如果它们都设置为宽度为“Auto”
,并且网格
设置为水平对齐=“Right”
,效果会更好
您还可以将设置为True
,这将为您提供有关实际发生情况的进一步线索
老实说,你的整个尝试似乎都是错误的。。。这是WPF,不是WinForms。您不应该创建UI元素并从代码隐藏处将它们添加到UI中。正确使用WPF时,您的需求更容易满足
首先将所有按钮添加到UI XAML中。接下来,在视图模型或代码隐藏中为每个对象添加一个bool
属性。然后,简单地使用数据绑定将这些bool
属性绑定到按钮
的visibility
属性。现在,要使每个按钮可见,只需将其相关bool
属性设置为true
:
<Button Content="Ok" Visibility="{Binding IsOkButtonVisible, Converter={StaticResource
BooleanToVisibilityConverter}}" />
没有你的代码的人很难猜到这里到底发生了什么。我猜您的行定义不正确。。。为什么要将一个设置为“*”
?如果它们都设置为宽度为“Auto”
,并且网格
设置为水平对齐=“Right”
,效果会更好
您还可以将设置为True
,这将为您提供有关实际发生情况的进一步线索
老实说,你的整个尝试似乎都是错误的。。。这是WPF,不是WinForms。您不应该创建UI元素并从代码隐藏处将它们添加到UI中。正确使用WPF时,您的需求更容易满足
首先将所有按钮添加到UI XAML中。接下来,在视图模型或代码隐藏中为每个对象添加一个bool
属性。然后,简单地使用数据绑定将这些bool
属性绑定到按钮
的visibility
属性。现在,要使每个按钮可见,只需将其相关bool
属性设置为true
:
<Button Content="Ok" Visibility="{Binding IsOkButtonVisible, Converter={StaticResource
BooleanToVisibilityConverter}}" />
谢谢您的解决方案Sheridan。我刚开始使用WPF,还在学习。在本例中,请将其设置为HorizontalAlignment=“Right”。谢谢您的解决方案Sheridan。我刚开始使用WPF,还在学习。在本例中,将其设置为HorizontalAlignment=“Right”。