c# - ListBox 使用 caliburn.micro 自动调整大小不正确

标签 c# wpf caliburn.micro

我有一个使用 caliburn.micro 的应用程序,我观察到一些奇怪的行为。我有一个包含两个列的网格,它们设置为同样宽。在这个网格中,有两个列表框(见下图)。

equally broad listboxes

现在,如果一个项目被添加到一个 ListBox 而另一个为空,则包含该项目的 ListBox 会变得与该项目一样宽,而另一个会变小,这意味着两个 ListBox 不再同样宽。

not equally broad listboxes

现在故事的奇怪部分来了。如果我在添加项目之前调整窗口大小一次,一切都会按预期进行。

expected result

我从一个较大的应用程序中提取了这个示例,在该应用程序中我观察到与 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/

相关文章:

c# - 如何从 AppSettings.Config 文件中获取键值?

C# WPF 在 CellEditEnding 事件后更改数据网格单元格背景

c# - 方法的通用版本与接口(interface)版本

c# - 如何获取 WPF 中组框中的控件列表

mvvm - 绑定(bind)Textbox详情时如何避免属性过多

c# - 当有 "this"参数时AssertWasCalled

c# - ServiceStack.Redis.RedisClient 取消订阅函数挂起

c# - 如何使用 TreeView 转换器从列表创建树结构?

wpf - 如何设置覆盖 WPF 中全局样式的特定控件元素设置?

c# - WP7 相当于 EmptyDataTemplate?