我想这是一个非常基本的问题,但为什么WPF不会在第二次更改属性(例如后台)而不是第一次点击?我使用ViewModel来存储我的UI中的值。因此,当用户单击“保存”按钮时,我可以轻松访问它们。
ViewModel包含几个属性,这里只是一个摘录:
using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;
using Drawing = System.Drawing;
using Media = System.Windows.Media;
using BarcodeLib;
namespace BarcodeEncoder
{
class BarcodeViewModel : INotifyPropertyChanged
{
// Some more properties
private Media.Brush foreColor;
public Media.Brush ForeColor
{
get { return this.foreColor; }
set
{
if (this.foreColor != value)
this.OnPropertyChanged("ForeColor");
this.foreColor = value;
}
}
private Media.Brush backColor;
public Media.Brush BackColor
{
get { return this.backColor; }
set
{
if (this.backColor != value)
this.OnPropertyChanged("BackColor");
this.backColor = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public BarcodeViewModel()
{
// Initialisation of the other properties
this.ForeColor = new Media.SolidColorBrush(Media.Colors.Black);
this.BackColor = new Media.SolidColorBrush(Media.Colors.White);
}
}
}
这是相关的XAML标记:
<StackPanel
x:Name="rootElement"
Orientation="Horizontal"
Height="Auto" Width="Auto">
<StackPanel.DataContext>
<local:BarcodeViewModel />
</StackPanel.DataContext>
<!-- Some more UI elements -->
<StackPanel
x:Name="pForeColor"
Background="{Binding ForeColor}"
Width="Auto" Height="23"
Margin="5,0,5,0"
MouseDown="pForeColor_MouseDown"/>
</StackPanel>
最后但并非最不重要的是MouseDown事件处理程序:
private void pForeColor_MouseDown(object sender, MouseButtonEventArgs e)
{
using (var cd = new Forms.ColorDialog())
{
if (cd.ShowDialog() == Forms.DialogResult.OK)
this.bcvm.ForeColor = new Media.SolidColorBrush()
{
Color = Media.Color.FromArgb(cd.Color.A, cd.Color.R, cd.Color.G, cd.Color.B)
};
}
}
因此,如果我尝试设置StackPanel pForeColor
的颜色,ColorDialog会打开,但是当单击“确定”时,面板不会改变颜色。只有在再次执行此操作并提交选择时,才会显示上一个选定的颜色。
那么有人可以告诉我这里有什么问题吗?
答案 0 :(得分:0)
好的......明白了!
错误是,在设置新颜色之前调用start_urls = (
'theunderminejournal.com/#eu/eredar/item/124442',
# ^ should be:
'http://theunderminejournal.com/#eu/eredar/item/124442',
)
Methode(以及OnPropertyChanged
事件)。
只需检查,如果设置了新值,则设置新值,然后引发事件