当 IsAsync = true 时,组合框 ItemTemplate 不刷新

问题描述 投票:0回答:1

我有一个组合框模板,其中一些字段需要很长时间才能返回其值。因此,我尝试为这些字段设置 IsAsync=true ,以便下拉列表应该快速打开。期望的结果是,设置了 IsAsync 的值应该稍后在准备好时显示,但是它们根本不会更新,它们仅在选择项目时显示。

<ComboBox.ItemTemplate>
     <DataTemplate>
         <DockPanel  HorizontalAlignment="Stretch" >
             <StackPanel Orientation="Horizontal" DockPanel.Dock="Right">                    
                     <TextBlock Text="{Binding NumNotAssigned,IsAsync=true}" />

看起来像这样(下拉部分): enter image description here

但应该看起来像这样(所选项目部分): when selected

wpf combobox
1个回答
0
投票

enter image description here

<ComboBox ItemsSource="{Binding OB_TEST}" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <DockPanel  HorizontalAlignment="Stretch" >
                
                <StackPanel Orientation="Horizontal" DockPanel.Dock="Right" >
                    <TextBlock Text="{Binding DateInfo,IsAsync=true}" Width="200"/>

                    <local:UC_ComboBox_Column2 ID="{Binding ID}"  />
                </StackPanel>
                
            </DockPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
public partial class MyViewModel
{
    public class ModelComboBox
    {
        public int ID { get; set; } = 0;
        public string DateInfo { get; set; } = string.Empty;
        public string CityInfo { get; set; } = string.Empty;
    }
    public ObservableCollection<ModelComboBox> OB_TEST { get; } = new ();
    public MyViewModel()
    {
        OB_TEST.Add(new ModelComboBox() { ID = 1,DateInfo= "Tuesday 16/01/2024", CityInfo= "Birmingham" });
        OB_TEST.Add(new ModelComboBox() { ID = 2, DateInfo = "Wednesday 10/01/2024", CityInfo = "London" });
        OB_TEST.Add(new ModelComboBox() { ID = 3, DateInfo = "Tuesday 23/01/2024", CityInfo = "Germany" });
    }
}

UC_ComboBox_Column2.xaml

<UserControl x:Class="WpfApp2.UC_ComboBox_Column2"
             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" 
             xmlns:local="clr-namespace:WpfApp2"
             mc:Ignorable="d" 
             d:DesignHeight="60" d:DesignWidth="300">
    <UserControl.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Margin" Value="20,3"/>
            <Setter Property="Padding" Value="5"/>
            <Setter Property="TextAlignment" Value="Center"/>
            <Setter Property="Foreground" Value="Yellow"/>
            <Setter Property="Background" Value="Black"/>
        </Style>
    </UserControl.Resources>
    <Grid>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Value_1,RelativeSource={RelativeSource AncestorType=local:UC_ComboBox_Column2}}" />
            <TextBlock Text="{Binding Value_2,RelativeSource={RelativeSource AncestorType=local:UC_ComboBox_Column2}}" />
            <TextBlock Text="{Binding Value_3,RelativeSource={RelativeSource AncestorType=local:UC_ComboBox_Column2}}" />
            <TextBlock Text="{Binding Value_4,RelativeSource={RelativeSource AncestorType=local:UC_ComboBox_Column2}}" />
        </StackPanel>
        <ProgressBar Margin="5" Visibility="{Binding ProgressBarVisibility,RelativeSource={RelativeSource AncestorType=local:UC_ComboBox_Column2}}" IsIndeterminate="True" Opacity="0.9"/>  
    </Grid>
</UserControl>

UC_ComboBox_Column2.cs

using System.Windows;
using System.Windows.Controls;

namespace WpfApp2
{
    public partial class UC_ComboBox_Column2 : UserControl
    {
        public UC_ComboBox_Column2()
        {
            InitializeComponent();
        }


        public int ID
        {
            get { return (int)GetValue(IDProperty); }
            set { SetValue(IDProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IDProperty =
            DependencyProperty.Register("ID", typeof(int), typeof(UC_ComboBox_Column2), new PropertyMetadata(0, ID_Changed));

        private static void ID_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(d is not null and UC_ComboBox_Column2 item)
            {
                item.LoadFromFakeDB((int)e.NewValue);
            }
        }

        private async void LoadFromFakeDB(int pID)
        {
            ProgressBarVisibility=Visibility.Visible;
            //to simulate a delay
            await Task.Delay(2000*pID+(pID*100));
            //-----------------------------------
            Value_1 = 20 + pID;
            Value_2 = 82 + pID*2;
            Value_3 = 65 + pID*3;
            Value_4 = 13 + pID*4;
            ProgressBarVisibility = Visibility.Collapsed;
        }

        public Visibility ProgressBarVisibility
        {
            get { return (Visibility)GetValue(ProgressBarVisibilityProperty); }
            set { SetValue(ProgressBarVisibilityProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ProgressBarVisibility.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ProgressBarVisibilityProperty =
            DependencyProperty.Register("ProgressBarVisibility", typeof(Visibility), typeof(UC_ComboBox_Column2), new PropertyMetadata(Visibility.Collapsed));


        #region Value_1
        public int Value_1
        {
            get { return (int)GetValue(Value_1Property); }
            set { SetValue(Value_1Property, value); }
        }

        // Using a DependencyProperty as the backing store for Value_1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Value_1Property =
            DependencyProperty.Register("Value_1", typeof(int), typeof(UC_ComboBox_Column2), new PropertyMetadata(0));
        #endregion

        #region Value_2
        public int Value_2
        {
            get { return (int)GetValue(Value_2Property); }
            set { SetValue(Value_2Property, value); }
        }

        // Using a DependencyProperty as the backing store for Value_2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Value_2Property =
            DependencyProperty.Register("Value_2", typeof(int), typeof(UC_ComboBox_Column2), new PropertyMetadata(0));
        #endregion

        #region Value_3
        public int Value_3
        {
            get { return (int)GetValue(Value_3Property); }
            set { SetValue(Value_3Property, value); }
        }

        // Using a DependencyProperty as the backing store for Value_3.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Value_3Property =
            DependencyProperty.Register("Value_3", typeof(int), typeof(UC_ComboBox_Column2), new PropertyMetadata(0));
        #endregion

        #region Value_4
        public int Value_4
        {
            get { return (int)GetValue(Value_4Property); }
            set { SetValue(Value_4Property, value); }
        }

        // Using a DependencyProperty as the backing store for Value_4.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Value_4Property =
            DependencyProperty.Register("Value_4", typeof(int), typeof(UC_ComboBox_Column2), new PropertyMetadata(0)); 
        #endregion
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.