WPF 图像在 foreach 语句时不显示

本文关键字:显示 语句 foreach 图像 WPF | 更新日期: 2023-09-27 17:56:15

我在WPF应用程序上遇到了一个非常奇怪的问题。让我清楚地解释我做了什么。

我有带有选定项的数据网格复选框,因此用户选择并单击加载按钮,然后记录将加载到数据库服务器。在此期间,我在将数据加载到数据库服务器时不断旋转图像。因为我有很多记录。

默认情况下,当数据加载属性更改为可见时,我将图像保持隐藏状态。它来自每个声明图像永远不会显示,或者默认情况下图像显示但从不旋转......对此有任何想法或帮助,我做错了..?

Xaml 代码...

<Button Content="Load" Height="23" HorizontalAlignment="Left" Margin="1042,83,0,0"
    Name="btnSaveData" Visibility="Hidden" VerticalAlignment="Top" Width="75"
    Cursor="Hand" Click="btnSaveData_Click" Foreground="Green" 
    Background="#FFB0D3D3" FontWeight="Bold" FontSize="14"/>
<Image Height="25" HorizontalAlignment="Left" Margin="1012,83,0,0" Name="imgSpin5"
    Stretch="None" RenderTransformOrigin="0.5,0.5" Visibility="Hidden"
    VerticalAlignment="Top" Width="24"
    Source="/LoadDataSource;component/Images/Spin5.png">
    <Image.RenderTransform>
        <RotateTransform x:Name="TransRotate" Angle="0"/>
    </Image.RenderTransform>
    <Image.Triggers>
        <EventTrigger RoutedEvent="Image.Loaded">
            <BeginStoryboard>
                <Storyboard TargetProperty="Angle">
                    <DoubleAnimation Storyboard.TargetName="TransRotate"
                        Storyboard.TargetProperty="Angle" By="360" 
                        Duration="0:0:1" AutoReverse="False" 
                        RepeatBehavior="Forever" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Image.Triggers>
</Image>

C# 代码..

MessageBoxResult result = MessageBox.Show("Do you want to Load Selected items?",
   "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Information);
if (result == MessageBoxResult.Yes)
{
    imgSpin5.Visibility = Visibility.Visible;
    foreach (CType ctp in dgAttributes.ItemsSource)
    {
        if (ctp.IsSelected)
            imgSpin5.Visibility = Visibility.Visible;
    }
}

WPF 图像在 foreach 语句时不显示

您可以尝试将可见性更新包装在 Dispatcher.Invoke 调用中,以强制将其强制到 UI 线程的顶部,并在后台工作线程上运行foreach

MessageBoxResult result = MessageBox.Show("Do you want to Load Selected items?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Information);
    if (result == MessageBoxResult.Yes)
    {
        imgSpin5.Visibility = Visibility.Visible;
        BackgroundWorker backgroundWorker = new BackgroundWorker();
        backgroundWorker.DoWork((s,e)=>{
            foreach (CType ctp in dgAttributes.ItemsSource)
            {
                if (ctp.IsSelected == true)
                {
                    Dispatcher.Invoke(() =>
                    {
                        imgSpin5.Visibility = Visibility.Visible;
                    });
                }
            }
        });
        backgroundWorker.RunWorkerAsync();
    }

if (result == MessageBoxResult.Yes) {

                BackgroundWorker backgroundWorker = new BackgroundWorker();
                backgroundWorker.DoWork += delegate(object s, DoWorkEventArgs args)
                {
                    foreach (CType ctp in dgAttributes.ItemsSource)
                    {
                        if (ctp.IsSelected == true)
                        {
                            Dispatcher.Invoke(() =>
                            {
                                imgSpin.Visibility = Visibility.Visible;
                            });
                        }
                    }
                };
                backgroundWorker.RunWorkerAsync();
                MessageBoxResult results = MessageBox.Show("Sucessfully Loaded..!", "Confirmation", MessageBoxButton.OK);
                imgSpin.Visibility = Visibility.Hidden;
            }