我想根据增加的参数创建一个用颜色填充内部的容器。根据值更改高度
例如我创建了下面的例子: 主窗口:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Border BorderBrush="Black" BorderThickness="1" Width="100" Height="200">
<Rectangle VerticalAlignment="Bottom" Height="{Binding Height}" Width="100" Fill="Red" MaxHeight="200"/>
</Border>
</Grid>
Engine.cs:
class Engine
{
public ViewModel viewModel = new ViewModel();
public void process()
{
Thread a = new Thread(() =>
{
while (viewModel.Height < 200)
{
ChangeHeight();
Thread.Sleep(1000);
}
});
a.IsBackground = true;
a.Start();
}
public void ChangeHeight()
{
viewModel.Height++;
}
}
视图模型是DataContext的。它工作的很好,但我认为比我做得更好。 此外,我需要在ChangeHeight()之间进行转换,以保持平滑,这意味着此处需要动画。
有没有什么好的例子或指导?
UPDATE 我添加视图模型的代码:
namespace WpfApplication1
{ 公共类视图模型:INotifyPropertyChanged的 { 私人INT m_height = 0; public int Height { get {return m_height; } set { m_height = value; NotifyPropertyChanged(“Height”); } }
#region "PropertyChanged Event"
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
您是否在ViewModel中实现了'INotifyPropertyChanged'?你可以将其代码添加到问题中吗? – CKII
你为什么觉得它不光滑?这里发生了什么。你可以添加更多的细节。 – Versatile
请参阅:http://stackoverflow.com/questions/3762576/wpf-backgroundworker-vs-dispatcher – Versatile