我在WPF应用程序上面临一个非常奇怪的问题。让我清楚地解释一下我做了什么。
我将带有选定项的Datagrid作为复选框,以便用户选择并单击“加载”按钮,然后该记录将加载到数据库服务器。在此期间,我在数据加载到数据库服务器时保持旋转图像。因为我有很多记录。
默认情况下,当数据加载属性更改为可见时,我将图像保持为隐藏状态。什么它来到foreach声明图像永远不会显示或如果它是可见性默认图像显示但从未旋转...任何想法或帮助这个我做错了..?
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;
}
}
答案 0 :(得分:0)
您可以尝试在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();
}
答案 1 :(得分:0)
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;
}