【发布时间】:2017-07-19 13:23:40
【问题描述】:
我想根据增加的参数创建一个用颜色填充内部的容器。
例如,我创建了以下示例: 主窗口:
<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++;
}
}
ViewModel 是数据上下文。它工作得很好,但我认为有些东西比我做的要好得多。 此外,我需要 ChangeHeight() 之间的传输平滑,这意味着这里需要动画。
有什么好的例子或指导吗?
更新 我正在添加视图模型代码:
namespace WpfApplication1
{ 公共类 ViewModel:INotifyPropertyChanged { 私人 int m_height = 0; 公共 int 高度 { 得到 { 返回 m_height; } 放 { m_height = 值; NotifyPropertyChanged("身高"); } }
#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 - 如果我将高度增加 15 而不是 1,它将跳到我提到的高度,我需要它顺利成长。