【问题标题】:ListBox within Popup - position and binding issue弹出窗口中的列表框 - 位置和绑定问题
【发布时间】:2023-04-05 23:45:01
【问题描述】:

以下是UI的代码和后面的代码。没有比这更多的代码了。

问题是

  1. 当我将值绑定到 ObservableCollection 并有一些延迟时,ListBox 不可见。
  2. ListBox 不在弹出窗口的中心。可以在 Design Viewer 中查看。

我尝试将ListBox 放在弹出窗口之外,效果很好。我需要在弹出窗口中放置列表框。

MainWindow.xaml

<Window x:Class="WPFTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFTest"
        mc:Ignorable="d"
        Title="TestWPF" MinHeight="500" MinWidth="600" 
        WindowStyle="SingleBorderWindow" WindowStartupLocation="CenterScreen">
    <Grid x:Name="mainContainer">

        <Popup x:Name="popup" IsOpen="False" Margin="10" PlacementTarget="{Binding ElementName=mainContainer}"
               Placement="Center" Height="300" Width="300">
            <ListBox ItemsSource="{Binding InstallationSummary}"> 
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="IsEnabled" Value="False"/>
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderThickness="0,0,0,1" 
                            Margin="5" Padding="2">
                            <TextBlock Text="{Binding}"
                                       TextWrapping="Wrap" Foreground="Black"/>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Popup>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;

namespace WPFTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<string> InstallationSummary;

        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += MainWindow_Loaded;

            InstallationSummary = new ObservableCollection<string>();

            this.DataContext = this;
        }

        async private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            popup.IsOpen = true;

            for (int i = 0; i < 25; i++)
            {
                await Task.Delay(1000);

                App.Current.Dispatcher.Invoke(() =>
                {
                    InstallationSummary.Add("New Item-" + i);
                });
            }
        }
    }
}

【问题讨论】:

    标签: c# wpf xaml popup wpf-controls


    【解决方案1】:

    尝试转换您的私有变量 ObservableCollection 安装总结 像这样的公共财产

    public ObservableCollection<string> InstallationSummary{ get set};
    

    应该可以正常工作..

    【讨论】:

    • 你能解释一下背后的原因吗?它也可以在 MVVM 模式下工作吗?
    • 背后的原因是Properties用于XAML中的数据绑定;字段本身无法做到这一点,因为它们没有实现 getter 和 setter。就 MVVM 模式而言.. 只需将 xaml 的数据上下文绑定到视图模型,MVVM 模式也可以实现相同的结果
    猜你喜欢
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多