我有一個小型的WPF應用程式,我正在使用Xceed的BusyIndicator。 我在動態更新加載資訊時遇到了一些問題,因為內容包含在一個DataTemplate中。 我所熟悉的資料系結或設定文本值的方法并不奏效。
。我做了一些研究 - 看起來其他人也遇到了這個問題。 這個問題似乎在這里得到了解答,但由于該答案不在背景關系中,因此我無法弄清楚它在我的代碼中如何作業。
這是我的示例代碼。
這是我的示例代碼,如果有人能幫助我理解我所缺少的東西,我將非常感激。這有一個額外的挑戰,即使用一個BackgroundWorker執行緒。 我使用這個執行緒是因為我預計這將是一個長期運行的行程--最終這個動作將啟動一個SQL作業,處理專案可能需要15分鐘。 我的計劃是讓該執行緒定期運行一個存盤程序,以獲得剩余專案的數量來處理和更新加載資訊。
MainWindow.xaml:
<Window x:Class="WPFTest.MainWindow"/span>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"/span>
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"/span>
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"/span>
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"/span>
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"。
xmlns:local="clr-namespace:WPFTest"/span>
mc:Ignorable="d"。
Title="MainWindow"/span> Height="450"/span> Width="800"/span>>
<xctk:BusyIndicator x:Name="AutomationIndicator"/span>>
<xctk:BusyIndicator.BusyContentTemplate>
<DataTemplate>
<StackPanel Margin="4">
<TextBlock Text="發送發票" FontWeight="大膽" HorizontalAlignment="中心"/>
<WrapPanel>
<TextBlock Text="剩余專案。"/>
<TextBlock x:Name="_ItemsRemaining" Text="{Binding Path=DataContext.ItemsRemaining, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}" /span>/>
</WrapPanel>
</StackPanel>
</DataTemplate>
</xctk:BusyIndicator.BusyContentTemplate>
<Grid>
<StackPanel>
<TextBlock Text="讓我們測驗一下這個東西" />
<Button x: Name="_testBtn" Content="開始" Width="100"/span> HorizontalAlignment="Left"/span> Click="testBtn_Click"/>
<TextBlock Text="{Binding ItemsRemaining}"/>
</StackPanel>
</Grid>
</xctk:BusyIndicator>
</Window>
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
使用System.Threading。
using System.Threading.Tasks;
使用System.Windows.Tasks; 使用System.Windows.Tasks; 使用System.Windows.Tasks;
使用System.Windows.Data。
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WPFTest
{
// <summary>
// MainWindow.xaml的互動邏輯。
// </summary>/span>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent()。
UpdateItemsRemaining(0)。
}
public class ItemCountDown
{
//O one Idea was to try and set a data binding variable
public string ItemsRemaining { get; set; }
}
public void UpdateItemsRemaining(int n)。
{
ItemCountDown s = new ItemCountDown();
{
s.ItemsRemaining = n.ToString()。
};
//this.AutomationIndicator.DataContext = s; 在啟動程序中起作用,但在執行緒作業者中不起作用。
}
private void testBtn_Click(object sender, RoutedEventArgs e)。
{
//Someone clicked the button, run the Test Status
TestStatus()。
}
public void TestStatus()
{
//通常我會啟動一個后臺作業者來運行一個回圈,在SQL中檢查該狀態。
BackgroundWorker getStatus = new BackgroundWorker()。
getStatus.DoWork = (o, ea) =>
{
//Normally there's a sql connection being opened to check a SQL Job, and then I run a loop that opens the connection to check.
//the status until it either fails or successfully ended.
//但是對于這個測驗,我只讓它運行15秒,倒數假的專案。
int fakeItems = 8;
do //
{
//Idea One - 寫到文本引數。 但在模板中找不到它
//Dispatcher.Invoke((Action)(() => _ItemsRemaining.Text = fakeItems));
//Idea two - 使用資料系結來更新值。 資料系結在資料模板外只是找到了作業,但在模板中被忽略了
UpdateItemsRemaining(fakeItems)。
//saken one from fake items and wait a second.
fakeItems--。
Thread.Sleep(1000)。
} while(fakeItems > 0)。
};
getStatus.RunWorkerCompleted = (o, ea) =>
{
//work done, end it.。
AutomationIndicator.IsBusy = false;
};
AutomationIndicator.IsBusy = true;
getStatus.RunWorkerAsync()。
}
}
感謝您的評論,我很感謝您的幫助和指導。
uj5u.com熱心網友回復:
將DataContext設定為你的ItemCountDown物件并實作INotifyPropertyChanged:
public class ItemCountDown : INotifyPropertyChanged<
{
private string _itemsRemaining;
public string ItemsRemaining
{
get { return _itemsRemaining; }
set { _itemsRemaining = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = ">) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(pertyName))。
}
public partial class MainWindow : Window
{
private readonly ItemCountDown s = new ItemCountDown()。
public MainWindow()
{
InitializeComponent()。
DataContext = s;
UpdateItemsRemaining(0)。
}
public void UpdateItemsRemaining(int n)。
{
s.ItemsRemaining = n.ToString()。
}
private void testBtn_Click(object sender, RoutedEventArgs e)。
{
TestStatus()。
}
public void TestStatus()
{
...
}
}
然后你可以在XAML標記的DataTemplate中直接系結到該屬性:
<TextBlock x:Name="_ItemsRemaining"/span> Text="{Binding Path=DataContext. ItemsRemaining, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"/span>/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/307896.html
標籤:
下一篇:<p>大家好,我需要一些幫助,讓下面的代碼發揮作用。我是按照<ahref="#"><imgsrc="https://img.codepudd
