如果在加载时更改内容,则StackPanel中的WPF标签不会调整大小

时间:2018-10-09 16:19:29

标签: c# wpf data-binding

请查看绑定的Label数据,该数据在加载时不会调整大小。之后,当在ListView中选择不同的项目时(但它们必须具有不同的长度!),它将起作用。

“调整大小”的定义: 通常,标签会自动调整其宽度以适合内容。在我的漏洞利用中,这不会发生。内容被切断。

当然必须是s。 th。为此,我通过AutoSelect的专用属性Item实现了自动选择机制。

如何在保留自动选择逻辑的同时解决此问题? 请不要选择多项选择的可能性,因为这与这个问题无关。


查看:

<Window x:Class="TestApp___WPF_ListView.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:TestApp___WPF_ListView"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="300">
    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel  Grid.Row="0" Grid.Column="0" Orientation="Horizontal" Margin="0 10">
            <Label Content="{Binding SelectedItemName}" BorderBrush="Red" BorderThickness="1" />
            <Label Content="Other Text" />
        </StackPanel>
        <ListView Grid.Row="1" Grid.Column="0" Name="listview" ItemsSource="{Binding TvItems}" SelectedItem="{Binding SelectedItem}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="IsSelected" Value="{Binding AutoSelect}" />
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>
    </Grid>
</Window>

ViewModel:

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace TestApp___WPF_ListView
{
    public class ViewModel : INotifyPropertyChanged
    {
        private Item m_SelectedItem = null;

        public event PropertyChangedEventHandler PropertyChanged;
        protected void DoPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public ObservableCollection<Item> Items { get; }

        public Item SelectedItem
        {
            get
            {
                return m_SelectedItem;
            }

            set
            {
                m_SelectedItem = value;
                DoPropertyChanged("SelectedItem");
                DoPropertyChanged("SelectedItemName");
            }
        }

        public string SelectedItemName { get { return m_SelectedItem?.Name ?? "null"; }
        }

        public ViewModel()
        {
            Items = new ObservableCollection<Item>();
            Items.Add(new Item("Item #1 asölkasd", false));
            Items.Add(new Item("Item #2 as", false));
            Items.Add(new Item("Item #3 asdköwowfialöafl", false));
            Items.Add(new Item("Item #4 akksad", true));
            Items.Add(new Item("Item #5 aöasölskfawelllkasdlk", false));
            Items.Add(new Item("Item #6 aslsdkföwl", false));
        }
    }
}

数据项:

namespace TestApp___WPF_ListView
{
    public class Item
    {
        public string Name { get; set; }
        public bool AutoSelect { get; set; }

        public Item(string p_Name, bool p_AutoSelect)
        {
            Name = p_Name;
            AutoSelect = p_AutoSelect;
        }
    }
}

当然,我花了几个小时才从真正的业务应用程序中找出问题的根本原因...;-)

1 个答案:

答案 0 :(得分:1)

如果在将视图模型从XAML加载到已加载的语句中时进行了更改,则可以解决此问题。问题是在视图之前创建了视图模型,因此没有通知告诉该视图要更新。一旦单击列表中的某个项目,视图更新即会收到通知事件,并且所有内容都将正确更新。

删除:

<Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>

添加:

 public MainWindow()
    {
        InitializeComponent();           
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = new ViewModel();
    }