我有一个使用 caliburn.micro 的应用程序,我观察到一些奇怪的行为。我有一个包含两个列的网格,它们设置为同样宽。在这个网格中,有两个列表框(见下图)。
现在,如果一个项目被添加到一个 ListBox 而另一个为空,则包含该项目的 ListBox 会变得与该项目一样宽,而另一个会变小,这意味着两个 ListBox 不再同样宽。
现在故事的奇怪部分来了。如果我在添加项目之前调整窗口大小一次,一切都会按预期进行。
我从一个较大的应用程序中提取了这个示例,在该应用程序中我观察到与 ItemControlls 和 TreeViews 相同的行为。在我的研究中,我用纯 WPF 重建了这个示例应用程序,一切正常。所以我认为这个问题一定与caliburn.micro有关。
这是 MainView 的 XAML:
<UserControl x:Class="WpfResizeErrorTest.Views.MainView"
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"
mc:Ignorable="d" Background="#FF3D3A3A">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox Name="LeftItems" Grid.Column="0" Grid.Row="0" Margin="0,0,5,0" />
<ListBox Name="RightItems" Grid.Column="1" Grid.Row="0" Margin="5,0,0,0"/>
<Button Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Name="AddItem" Height="30" Margin="5,5,5,5" Content="Add Item" />
</Grid>
</UserControl>
以及对应 View 模型的代码:
public class MainViewModel : Screen
{
public MainViewModel()
{
this.LeftItems = new BindableCollection<string>();
this.RightItems = new BindableCollection<string>();
}
public BindableCollection<string> LeftItems { get; set; }
public BindableCollection<string> RightItems { get; set; }
public void AddItem()
{
this.LeftItems.Add("This is a long text which is usually longer than the list box it comes in.");
}
}
有人知道问题出在哪里吗?
最佳答案
我的一位同事找到了解决方案。 Caliburn.micro 更改了 Window
对象的 SizeToContent
属性的值。您必须在 Bootstrap 中执行此操作:
public class Bootstrapper : BootstrapperBase
{
public Bootstrapper()
{
this.Initialize();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
base.OnStartup(sender, e);
var settings = new Dictionary<string, object>
{
{ "SizeToContent", SizeToContent.Manual }
};
this.DisplayRootViewFor<MainViewModel>(settings);
}
}
一切都按预期进行。
关于c# - ListBox 使用 caliburn.micro 自动调整大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30732650/