绑定问题 [英] Problem with binding
问题描述
我遇到了将值绑定到控件的问题。我在其他窗口检查过,我认为我也是这样做的。
我想在主窗口打开之前显示类似于从App加载窗口的内容。
InitizalizationWindow:
I'm having a problem with binding values to controls. I checked in other windows I have and I think I'm doing it the same way.
I want to show something like loading window from App before main window will open.
InitizalizationWindow:
public InitializationWindow()
{
...
InitializationWindowClass.Progress = new InitializationWindowClass();
this.mainSP.DataContext = InitializationWindowClass.Progress;
}
和部分xaml:
and part of xaml:
<StackPanel Name="mainSP" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Margin="0, 0, 0, 10">
<TextBlock x:Name="tblProgress" FontSize="14" Text="{Binding ProgressText}" TextAlignment="Center" TextWrapping="Wrap" />
<Grid>
<telerik:RadProgressBar x:Name="progress" Value="{Binding ProgressValue}" telerik:StyleManager.Theme="Summer" Height="25" IsIndeterminate="False" />
<Label x:Name="lblPercent" FontWeight="Bold" Content="{Binding ProgressValueString}" HorizontalAlignment="Center" VerticalContentAlignment="Center" />
</Grid>
</StackPanel>
InitializationWindowClass:
InitializationWindowClass:
public class InitializationWindowClass : INotifyPropertyChanged
{
public static InitializationWindowClass Progress { get; set; }
private string progressText = String.Empty, progressValueString = String.Empty;
private int progressValue = 0;
public event PropertyChangedEventHandler PropertyChanged;
public string ProgressText
{
get
{
return progressText;
}
set
{
progressText = value;
NotifyPropertyChanged("ProgressText");
}
}
public string ProgressValueString
{
get
{
return progressValueString;
}
set
{
progressValueString = value;
NotifyPropertyChanged("ProgressValueString");
}
}
public int ProgressValue
{
get
{
return progressValue;
}
set
{
progressValue = value;
ProgressValueString = String.Format("{0}%", progressValue);
NotifyPropertyChanged("ProgressValue");
NotifyPropertyChanged("ProgressValueString");
}
}
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
和App.xaml.cs的一部分:
and part of App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
InitializationWindow iw = new InitializationWindow();
iw.Show();
InitializationWindowClass.Progress.ProgressValue = Convert.ToInt32(((decimal)count / (decimal)sum) * 100);
InitializationWindowClass.Progress.ProgressText = "Some text";
...
...
InitializationWindowClass.Progress.ProgressValue = Convert.ToInt32(((decimal)count / (decimal)sum) * 100);
InitializationWindowClass.Progress.ProgressText = "New text";
...
...
}
我检查过,当我从App.xaml.cs更改即ProgressValue时,值正在改变 - 转到
I checked and when I'm changing i.e. ProgressValue from App.xaml.cs, the value is changing - going to
get
{
return progressValue;
}
所以问题是 - 我做错了什么?
So the question is - what I'm doing wrong?
推荐答案
请尝试下面的app.xaml.cs。
ProgressBar正在增长并显示价值。
所以你的OnStartup方法阻止了UI线程。
这不是数据绑定问题。 />
Please try the app.xaml.cs below.
ProgressBar is growing and value is shown.
So your OnStartup method blocked the UI thread.
It is not a data binding problem.
public partial class App : Application
{
public App()
{
this.Startup += App_Startup;
}
void App_Startup(object sender, StartupEventArgs e)
{
InitializationWindow iw = new InitializationWindow();
iw.Show();
Task task = new Task(Test);
task.Start();
}
void Test()
{
for (int i = 0; i <= 100; i++)
{
InitializationWindowClass.Progress.ProgressValue = Convert.ToInt32(((decimal)i / (decimal)100) * 100);
InitializationWindowClass.Progress.ProgressText = "Some text";
Thread.Sleep(100);
}
}
}
这篇关于绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!