C# WPF-如何绑定到UserControl中的UserControl属性,以便子级更新父级?
我试图创建一个基本的颜色变化用户控制使用我的自定义滑块控制,标签滑动。我可以通过代码设置颜色,但是如果我移动滑块或在文本框中输入文本,它不会更新颜色 LabelSlider XAML:C# WPF-如何绑定到UserControl中的UserControl属性,以便子级更新父级?,c#,wpf,xaml,data-binding,user-controls,C#,Wpf,Xaml,Data Binding,User Controls,我试图创建一个基本的颜色变化用户控制使用我的自定义滑块控制,标签滑动。我可以通过代码设置颜色,但是如果我移动滑块或在文本框中输入文本,它不会更新颜色 LabelSlider XAML: <Slider x:Name="ucSlider" x:FieldModifier="private" Margin="{Binding SliderSpacing, FallbackValue=5, Converter={StaticResource DoubleToMargin}}" VerticalA
<Slider x:Name="ucSlider" x:FieldModifier="private" Margin="{Binding SliderSpacing, FallbackValue=5, Converter={StaticResource DoubleToMargin}}" VerticalAlignment="Center" Grid.Column="1" Width="{Binding SliderWidth}" MaxWidth="{Binding SliderWidth}" FontFamily="Segoe UI" Value="{Binding Value, Mode=TwoWay}" SmallChange="1" Maximum="255"/>
<TextBox x:Name="ucTextBox" x:FieldModifier="private" Text="{Binding Value, TargetNullValue=0, Mode=TwoWay}" Margin="{Binding TextBoxSpacing, FallbackValue=5, Converter={StaticResource DoubleToMargin}}" Grid.Column="2" PreviewKeyDown="LabelSlider_PreviewKeyDown" PreviewTextInput="LabelSlider_PreviewTextInput" MaxLength="3" Width="30" MaxWidth="30" FontFamily="Segoe UI" TextChanged="LabelSlider_TextChanged"/>
颜色选择XAML:
<UserControl x:Class="Homuli.UserControls.ColourSelection"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Homuli.UserControls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" x:Name="colourSelection">
<StackPanel Margin="5,0,0,0" DataContext="{Binding ElementName=colourSelection}">
<local:LabelSlider Label="R" SliderSpacing="6" SliderWidth="100" Value="{Binding R, FallbackValue=255, TargetNullValue=0}" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,-4"/>
<local:LabelSlider Label="G" SliderSpacing="5" SliderWidth="100" Value="{Binding G, FallbackValue=0, TargetNullValue=0}" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,-4"/>
<local:LabelSlider Label="B" SliderSpacing="6" SliderWidth="100" Value="{Binding B, FallbackValue=0, TargetNullValue=0}" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,-4"/>
<StackPanel Orientation="Horizontal">
<local:LabelTextBox Label="Hex" Spacing="15" TextBoxWidth="50" Text="{Binding Hex}" MaxLength="6" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Rectangle Fill="{Binding Colour}" Height="26" Width="26" Margin="26,0,0,0" Stroke="Black" />
</StackPanel>
</StackPanel>
颜色选择代码隐藏:
public int Value
{
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register
(
"Value",
typeof(int),
typeof(LabelSlider),
new PropertyMetadata(null)
);
public partial class ColourSelection : UserControl
{
public ColourSelection()
{
InitializeComponent();
}
public SolidColorBrush Colour
{
get { return (SolidColorBrush)GetValue(ColourProperty); }
set { SetValue(ColourProperty, value); }
}
public static readonly DependencyProperty ColourProperty = DependencyProperty.Register
(
"Colour",
typeof(SolidColorBrush),
typeof(ColourSelection),
new PropertyMetadata(null)
);
public int R
{
get { return (int)GetValue(RProperty); }
set
{
if (value <= 255 && value >= 0)
{
SetValue(RProperty, value);
UpdateColour();
}
}
}
public static readonly DependencyProperty RProperty = DependencyProperty.Register
(
"R",
typeof(int),
typeof(ColourSelection),
new PropertyMetadata()
);
public int G
{
get { return (int)GetValue(GProperty); }
set
{
if (value <= 255 && value >= 0)
{
SetValue(GProperty, value);
UpdateColour();
}
}
}
public static readonly DependencyProperty GProperty = DependencyProperty.Register
(
"G",
typeof(int),
typeof(ColourSelection),
new PropertyMetadata(null)
);
public int B
{
get { return (int)GetValue(BProperty); }
set
{
if (value <= 255 && value >= 0)
{
SetValue(BProperty, value);
UpdateColour();
}
}
}
public static readonly DependencyProperty BProperty = DependencyProperty.Register
(
"B",
typeof(int),
typeof(ColourSelection),
new PropertyMetadata(null)
);
void UpdateColour()
{
Colour = new SolidColorBrush(Color.FromRgb((byte)R, (byte)G, (byte)B));
}
}
公共部分类颜色选择:UserControl
{
公众选择()
{
初始化组件();
}
公共色刷颜色
{
获取{return(SolidColorBrush)GetValue(colorProperty);}
set{SetValue(colorProperty,value);}
}
公共静态只读DependencyProperty colorProperty=DependencyProperty.Register
(
“颜色”,
类型(SolidColorBrush),
类型(颜色选择),
新属性元数据(空)
);
公共INTR
{
获取{return(int)GetValue(RProperty);}
设置
{
如果(值=0)
{
设置值(rpproperty,value);
updateColor();
}
}
}
公共静态只读DependencyProperty RProperty=DependencyProperty.Register
(
“R”,
类型(int),
类型(颜色选择),
新属性元数据()
);
公共INTG
{
获取{return(int)GetValue(GProperty);}
设置
{
如果(值=0)
{
设置值(GProperty,value);
updateColor();
}
}
}
公共静态只读DependencyProperty GProperty=DependencyProperty.Register
(
“G”,
类型(int),
类型(颜色选择),
新属性元数据(空)
);
公共int B
{
获取{return(int)GetValue(BProperty);}
设置
{
如果(值=0)
{
设置值(b属性,值);
updateColor();
}
}
}
公共静态只读DependencyProperty BProperty=DependencyProperty.Register
(
“B”,
类型(int),
类型(颜色选择),
新属性元数据(空)
);
void updateColor()
{
Color=新的SolidColorBrush(Color.FromRgb((字节)R,(字节)G,(字节)B));
}
}
任何帮助都将不胜感激。当DP发生变化时,它通过静态对象
BProperty
发生。代码设置器
if (value <= 255 && value >= 0)
{
SetValue(RProperty, value);
UpdateColour();
}
另外,我不建议像这样替换userControl中的dataContext=“{Binding ElementName=ColorSelection}”。在绑定中使用
ElementName
:
<local:LabelSlider Label="R" SliderSpacing="6" SliderWidth="100"
Value="{Binding R, ElementName=colourSelection, FallbackValue=255, TargetNullValue=0}"
当DP发生变化时,它通过静态对象BProperty
发生。代码设置器
if (value <= 255 && value >= 0)
{
SetValue(RProperty, value);
UpdateColour();
}
另外,我不建议像这样替换userControl中的dataContext=“{Binding ElementName=ColorSelection}”
。在绑定中使用ElementName
:
<local:LabelSlider Label="R" SliderSpacing="6" SliderWidth="100"
Value="{Binding R, ElementName=colourSelection, FallbackValue=255, TargetNullValue=0}"
我之前也尝试过类似的方法,但没有效果。但是,它现在工作得很好;也许我做错了什么。谢谢刚刚发现我之前做错了什么。。。我使用的是“false”而不是“0”。愚蠢的错误…我之前也试过类似的方法,但没有效果。但是,它现在工作得很好;也许我做错了什么。谢谢刚刚发现我之前做错了什么。。。我使用的是“false”而不是“0”。愚蠢的错误。。。