我特別需要使用System.Drawing.Graphics
作爲圖像控件的ImageSource。該圖形將通過滑塊進行更新,該滑塊的值將數據綁定到作爲構建圖形模型的對象。在滑塊和對象之間進行簡單的數據綁定以更新WPF中的圖形
我已經設置了什麼,我想完成一個最小的工作版本,並創造了儘可能多的「綁定基礎設施」盡我所能,但已經停止了事情開始讓我迷惑。我的代碼中有剛的主窗口(XAML和代碼後面)和RADIUS類:
主窗口:
<Window x:Class="MinimalUpdateableDrawing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
WindowState="Maximized">
<DockPanel>
<Slider x:Name="SizeSlider" DockPanel.Dock="Bottom" />
<Image x:Name="figure" Width="800" Height="600" />
</DockPanel>
</Window>
主窗口隱藏代碼:
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
namespace MinimalUpdateableDrawing {
public partial class MainWindow : Window {
Radius radius_instance;
public MainWindow() {
InitializeComponent();
radius_instance = new Radius();
this.Loaded +=new RoutedEventHandler(DrawCircle);
}
void DrawCircle(object sender, RoutedEventArgs e) {
int radius = radius_instance.Value;
using (var bmp = new Bitmap((int)figure.Width, (int)figure.Height)) {
using (var g = Graphics.FromImage(bmp)) {
g.FillEllipse(System.Drawing.Brushes.Blue,
(int)figure.Width/2-radius,
(int)figure.Height/2-radius,
radius*2, radius*2);
}
using(MemoryStream ms = new MemoryStream()) {
bmp.Save(ms, ImageFormat.Bmp);
ms.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
figure.Source = bitmapImage;
}
}
}
}
}
半徑類:
using System;
using System.ComponentModel;
namespace MinimalUpdateableDrawing
{
class Radius : INotifyPropertyChanged {
int _value = 100;
public int Value {
get { return _value; }
set {
_value = value;
OnPropertyChanged("Value");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string p) {
throw new NotImplementedException();
}
}
}
如果有人可以建議我應該怎麼做才能實現SizeSlider
和01之間的綁定,所以當我移動滑塊時圖像更新,這將讓我去!
爲什麼你不在MainWindow類中創建一個'Radius'屬性,並將Slider的值綁定到該屬性? – Clemens 2013-04-10 21:15:26
@Clemens這會違反模型/視圖分離,我認爲。我需要的圖紙,以反映更新充當模特,因爲會有我的程序等元素將引用這個對象,應該是不知道該視圖的對象。 – heltonbiker 2013-04-10 23:10:34