2012-06-04 60 views
1

我有代码的自定义用户控件:绑定异常,但我不知道为什么?

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> 

所以,如果我启动它,我得到一个异常的东西与值绑定是错误的。我也尝试了评论版(价值属性)。有没有例外,但如果我设置该属性的值我的滑块不会更改。

有没有人有任何想法为什么?我从来没有做过这样的事情:(

+3

请扩展您的问题,哪个绑定失败?也许提供你的调试窗口的输出或尝试使用Snoop来显示绑定错误 – Dominik

回答

3

有下面的依赖财产申报错误:

public static readonly DependencyProperty ValueProperty = 
    DependencyProperty.Register("Value", typeof(double), typeof(AudioControl), new UIPropertyMetadata(0)); 

这里的问题是,你定义的依赖属性为double类型的,但是给它的默认值0这是一个int尝试改变00.0

我跑你的代码,遇到异常最里面的异常包含以下信息:。

默认值类型与属性类型“值”不匹配。

我将上述行中的0更改为0.0,问题就消失了。

+0

+1我最喜欢的WPF错误之一。 – dowhilefor

+0

嗯,听起来有趣,我会尝试它 –

相关问题