我有一个带有该代码的自定义用户控件:
public partial class AudioControl : UserControl
{
public AudioControl()
{
InitializeComponent();
Value = 0.1;
DataContext = this;
}
public event RoutedPropertyChangedEventHandler<double> ValueChanged = delegate { };
public int TextWidth
{
get { return (int)GetValue(TextWidthProperty); }
set { SetValue(TextWidthProperty, value); }
}
public int SliderWidth
{
get { return (int)GetValue(SliderWidthProperty); }
set { SetValue(SliderWidthProperty, value); }
}
public string Header
{
get { return (string)GetValue(TextBlock.TextProperty); }
set { SetValue(TextBlock.TextProperty, value); }
}
//public double Value
//{
// get { return (double)GetValue(Slider.ValueProperty); }
// set { SetValue(Slider.ValueProperty, value); }
//}
public double Value
{
get {
return (double)GetValue(ValueProperty);
}
set {
SetValue(ValueProperty, value);
}
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double), typeof(AudioControl), new UIPropertyMetadata(0));
public static readonly DependencyProperty TextWidthProperty =
DependencyProperty.Register("TextWidth", typeof(int), typeof(AudioControl), new UIPropertyMetadata(0));
public static readonly DependencyProperty SliderWidthProperty =
DependencyProperty.Register("SliderWidth", typeof(int), typeof(AudioControl), new UIPropertyMetadata(0));
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
ValueChanged(this, e);
}
}
这个XAML:
<UserControl x:Class="Controller.Audio.AudioControl"
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:Audio="clr-namespace:Controller.Audio"
mc:Ignorable="d"
d:DesignHeight="23" d:DesignWidth="500" x:Name="audioControl">
<UserControl.Resources>
<Audio:DoubleToIntConverter x:Key="doubleToInt"/>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding TextWidth}"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="{Binding SliderWidth}"/>
</Grid.ColumnDefinitions>
<Label x:Name="txtBlock" Margin="0,0,10,0">
<Binding Path="Header"/>
</Label>
<Label Grid.Column="1" Content="{Binding ElementName=slider, Path=Value, Converter={StaticResource doubleToInt}}"/>
<Slider x:Name="slider" Grid.Column="2" Style="{StaticResource Office2010SilverSliderStyle}"
Value="{Binding Path=Value, ElementName=audioControl}" Minimum="0.0" Maximum="1.0" LargeChange="0.25" TickFrequency="0.01" ValueChanged="Slider_ValueChanged"/>
</Grid>
因此,如果我启动它,我会得到一个例外,即带有值绑定的东西是错误的。我也尝试了注释版本(value-Property)。没有例外但是如果我设置了该属性的值,我的滑块不会改变。
有谁知道为什么?我从未做过这样的事情:(
答案 0 :(得分:3)
以下依赖项属性声明中存在错误:
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double), typeof(AudioControl), new UIPropertyMetadata(0));
这里的问题是您将依赖项属性定义为double
类型,但为其提供了0
的默认值int
。尝试将0
更改为0.0
。
我运行了你的代码并遇到了异常。最里面的例外包含以下消息:
默认值类型与属性'Value'的类型不匹配。
我将上面一行中的0
更改为0.0
,问题就消失了。