使用UIPropertyMetadata写一个Brush的依赖属性。

 System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
if (ResSyncState == ResSyncState.Default)//3秒错误信息在进度条显示出来的时候不出现,否则与进度条的状态提示有冲突
{
if (ResFileState == ResFileState.Local)
{
ErroColor = new SolidColorBrush(Color.FromArgb(255, 40, 138, 221));
SyncBtnToolTip = "本地有新课件,可同步至云端";
ShowSbErooInfo(SyncBtnToolTip);
}
else if (ResFileState == ResFileState.LocalNew)
{
ErroColor = new SolidColorBrush(Color.FromArgb(255, 40, 138, 221));
SyncBtnToolTip = "本地课件有改动,可同步至云端";
ShowSbErooInfo(SyncBtnToolTip);
}
else if (ResFileState == ResFileState.Net)
{
ErroColor = new SolidColorBrush(Color.FromArgb(255, 255, 68, 68));
SyncBtnToolTip = "云端有新课件,可同步至本地";
ShowSbErooInfo(SyncBtnToolTip);
}
else if (ResFileState == ResFileState.NetNew)
{
ErroColor = new SolidColorBrush(Color.FromArgb(255, 255, 68, 68));
SyncBtnToolTip = "云端课件有改动,可同步至本地";
ShowSbErooInfo(SyncBtnToolTip);
}
}
}));
        private Brush _erroColor;

        public Brush ErroColor
{
get { return _erroColor; }
set { _erroColor = value;
ErroColor.Freeze();//不加这个会 必须在与 DependencyObject 相同的线程上创建 DependencySource。
this.RaisePropertyChanged(r => r.ErroColor); }
}

使用PropertyMetadata的话Binding 绑定ErroColor或者直接写一个色值,界面不会变化。

        /// <summary>
/// 异常文本颜色
/// </summary>
public static readonly DependencyProperty ErroColorProperty =
DependencyProperty.Register("ErroColor", typeof(Brush), typeof(BusyPrompt), new UIPropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 255, 68, 68)))); public Brush ErroColor
{
get { return (Brush)GetValue(ErroColorProperty); }
set { SetValue(ErroColorProperty, value); }
}
05-11 13:17