因此,我创建了一个包含滑块,scrollBars和IntegerUpDowns的设计,这些滑块,scrollBars和IntegerUpDowns都绑定到gheter,它们同时移动并且具有相同的值。我每个都有,每个ARGB一个。我在中间有一个stackPanel,在修改任何“工具”时,应更改BackgroundColor。
就我所想,我只需要了解其中一个工具值即可通过它们提供的数据来设置背景...但是我如何实现呢?
到目前为止,我已经对此进行了编码:
public partial class MainWindow : Window
{
SolidColorBrush brush;
public MainWindow()
{
InitializeComponent();
brush = new SolidColorBrush();
brush.Color = Color.FromArgb(0, 0, 0, 0);
stkColor.Background = brush;
}
private void scbScrollA_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
{
brush.Color.A = scbScrollA.Value(); //doesn't work
}
private void scbScrollR_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
{
}
private void scbScrollG_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
{
}
private void scbScrollB_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
{
}
}
正如我所说,仅通过了解滚动数据,因为我已将Bindings与所有其他工具一起使用,因此我可以设法设置颜色并在每次修改任何数据时刷新。
我的绑定:
<xctk:IntegerUpDown Grid.Column="7" Grid.Row="0" x:Name="iudB" Increment="10" Minimum="0" Maximum="255"
Value="{Binding ElementName=scbScrollB, Path=Value}"
/>
<ScrollBar Grid.Column="1" Grid.Row="6" Grid.RowSpan="2" Width="Auto" Orientation="Horizontal"
Minimum="0" Maximum="255" x:Name="scbScrollB"
Scroll="scbScrollB_Scroll" SmallChange="1"
LargeChange="10" Value="{Binding ElementName=sliderB, Path=Value}"
/>
<Slider
Grid.Column="6" Grid.Row="1" Grid.RowSpan="1"
Orientation="Vertical"
LargeChange="10"
Maximum="255"
SmallChange="1"
TickPlacement="TopLeft"
Minimum="0"
TickFrequency="25"
x:Name="sliderB"
/>
滑块没有绑定,因为据我所知它们是双向的。他们都没有给出错误。
答案 0 :(得分:1)
创建一个视图模型,该模型具有用于Color的四个分量的四个字节属性和用于生成的颜色值的Color
属性。
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private byte alpha;
private byte red;
private byte green;
private byte blue;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void SetColorComponent(ref byte field, byte value, string propertyName)
{
if (field != value)
{
field = value;
NotifyPropertyChanged(propertyName);
NotifyPropertyChanged(nameof(Color));
}
}
public byte Alpha
{
get { return alpha; }
set { SetColorComponent(ref alpha, value, nameof(Alpha)); }
}
public byte Red
{
get { return red; }
set { SetColorComponent(ref red, value, nameof(Red)); }
}
public byte Green
{
get { return green; }
set { SetColorComponent(ref green, value, nameof(Green)); }
}
public byte Blue
{
get { return blue; }
set { SetColorComponent(ref blue, value, nameof(Blue)); }
}
public Color Color
{
get { return Color.FromArgb(alpha, red, green, blue); }
}
}
将视图模型的实例分配给窗口的DataContext,并将Sliders,ScrollBars等绑定到视图模型属性。
<Window ...>
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<StackPanel>
<StackPanel.Background>
<SolidColorBrush Color="{Binding Color}"/>
</StackPanel.Background>
<Slider Minimum="0" Maximum="255" Value="{Binding Alpha}"/>
<Slider Minimum="0" Maximum="255" Value="{Binding Red}"/>
<Slider Minimum="0" Maximum="255" Value="{Binding Green}"/>
<Slider Minimum="0" Maximum="255" Value="{Binding Blue}"/>
<ScrollBar Minimum="0" Maximum="255" Value="{Binding Alpha}" Orientation="Horizontal"/>
<ScrollBar Minimum="0" Maximum="255" Value="{Binding Red}" Orientation="Horizontal"/>
<ScrollBar Minimum="0" Maximum="255" Value="{Binding Green}" Orientation="Horizontal"/>
<ScrollBar Minimum="0" Maximum="255" Value="{Binding Blue}" Orientation="Horizontal"/>
</StackPanel>
</Window>