本文介绍了如何更新wpf应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在wpf:
我创建了一个滑块并将其绑定到窗口背景。其代码为:
in wpf:
I created a slider and bind it to window background.here is the code:
Value="{Binding Path=Background.Color.B, ElementName=windowhelp, Mode=TwoWay}"
现在当我运行窗口并更改滑块值时,窗口背景不会变化!
如何在更改滑块值时更改窗口背景?
now when I run window and change the slider value , window background does not ghange!
how can I change window background when I change the slider value?
推荐答案
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525" x:Name="window" removed="LightBlue">
<Window.Resources>
<local:BrushToIntConverter x:Key="BrushToIntConverter" />
</Window.Resources>
<DockPanel>
<Slider DockPanel.Dock="Top" Minimum="0" Maximum="256" Value="{Binding Path=Background, ElementName=window, Mode=TwoWay, Converter={StaticResource ResourceKey=BrushToIntConverter}, ConverterParameter=R}"/>
<Slider DockPanel.Dock="Top" Minimum="0" Maximum="256" Value="{Binding Path=Background, ElementName=window, Mode=TwoWay, Converter={StaticResource ResourceKey=BrushToIntConverter}, ConverterParameter=G}"/>
<Slider DockPanel.Dock="Top" Minimum="0" Maximum="256" Value="{Binding Path=Background, ElementName=window, Mode=TwoWay, Converter={StaticResource ResourceKey=BrushToIntConverter}, ConverterParameter=B}"/>
</DockPanel>
</Window>
R,G,B三个傻瓜。
和刷到Int转换器:
Three silders for R,G,B.
And the Brush to Int Converter:
[ValueConversion(typeof(System.Windows.Media.Brush), typeof(int))]
public class BrushToIntConverter : IValueConverter
{
protected System.Windows.Media.Color m_Color = System.Windows.Media.Colors.White;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
System.Windows.Media.SolidColorBrush brush = value as System.Windows.Media.SolidColorBrush;
m_Color = brush.Color;
switch (parameter as string)
{
case "R":
return m_Color.R;
case "G":
return m_Color.G;
case "B":
return m_Color.B;
}
return 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
int intValue = (int)Math.Round((double)value);
byte r = m_Color.R;
byte g = m_Color.G;
byte b = m_Color.B;
switch (parameter as string)
{
case "R":
r = (byte)intValue;
break;
case "G":
g = (byte)intValue;
break;
case "B":
b = (byte)intValue;
break;
}
m_Color = new System.Windows.Media.Color() { R = r, G = g, B = b, A = m_Color.A };
return new System.Windows.Media.SolidColorBrush(m_Color);
}
}
这篇关于如何更新wpf应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!