我想根據增加的參數創建一個用顏色填充內部的容器。根據值更改高度
例如我創建了下面的例子: 主窗口:
<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