开发者

WPF Binding doesn't seem to work but I'm not getting any error messages in the Output window

I've got a dialog box that is opened by my main window. Among other things, it has a custom control I wrote that is supposed to control the volume of any MediaElement controls in the program.

The volume control contains 2 buttons and a ProgressBar. It has a Volume DependencyProperty. When you click on the volume up button, the value of the Volume property increases by 5%; similarly, clicking on the volume down button decreases the volume by 5%. This is reflected in the ProgressBar. I'm confident the ProgressBar is working, but I won't rule out issues in there.

The dialog box has its own Volume DependencyProperty, as does the main window, for that matter. I tried to bind the Volume property of the Volume control in the Dialog to the dialog's Volume property. I even added a text field to the dialog to tell me what the value of the Volume property is. I figured that I needed this to make sure the dialog's volume was really changing. It doesn't seem to be changing at all.

Here's an excerpt of the xaml for the dialog box. It's missing some things that aren't related to this problem.

<Window x:Class="CarSystem.SettingsDialog" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:c="clr-namespace:CarSystem"
        xmlns:cs="clr-namespace:CarSystem.CustomControls;assembly=CustomControls" 
        Closed="SettingsDialog_Closed"
        Height="300" 
        ResizeMode="NoResize" 
        Title="Settings" 
        Width="550" 
        WindowStartupLocation="CenterOwner" 
        WindowStyle="None">

    <StackPanel>

        <StackPanel Grid.Column="2" Orientation="Horizontal">
            <TextBlock FontSize="20" 
                       FontWeight="Bold" 
                       Foreground="White" 
                       Margin="10" 
                       Text="Volume: " 
                       VerticalAlignment="Center" />
            <TextBlock FontSize="20" 
                       FontWeight="Bold" 
                       Foreground="White" 
                       Margin="10" 
                       Text="{Binding Mode=OneWay, Path=Volume, RelativeSource={RelativeSource AncestorType={x:Type c:SettingsDialog}}}" 
                       VerticalAlignment="Center" />
        </StackPanel>

        <StackPanel>

            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <cs:VolumeControl Grid.Row="0" 
                              Height="60" 
                                  HorizontalAlignment="Left" 
                                  Margin="20,0,0,0" 
                                  Volume="{Binding Mode=OneWay, Path=Volume, RelativeSource={RelativeSource AncestorType={x:Type c:SettingsDialog}}}" 
                                  Width="325" />

                <Button Background="#FF3C4B66" 
                        Click="CloseButton_Click" 
                        Content="Close" 
                        FontSize="20" 
                        FontWeight="Bold" 
                        Foreground="White" 
                        Grid.Row="1" 
                        Height="50"
                        HorizontalAlignment="Right" 
                        Margin="0,5,20,5" 
                        Width="125" />
                </Grid>
            </StackPanel>
        </Border>

    </StackPanel>

</Window>

Here's the code-behind for the Volume control:

public partial class VolumeControl : Control {

    public static readonly DependencyProperty VolumeProperty = 
        DependencyProperty.Register( "Volume", typeof( double ), typeof( VolumeControl ), new FrameworkPropertyMetadata( 0.50 ) );

    public double Volume {
        get { return (double) GetValue( VolumeProperty ); }
        set { SetValue( VolumeProperty, value ); }
    }

    public VolumeControl() {
        Loaded += WasLoaded;
    }

    static VolumeControl() {
        DefaultStyleKeyProperty.OverrideMetadata( typeof( VolumeControl ), new FrameworkPropertyMetadata( typeof( VolumeControl ) ) );
    }

    private void VolumeDown_Click( object sender, RoutedEventArgs e ) {
        Volume = Math.Max( 0, Volume - 0.05 );
开发者_如何学Go    }

    private void VolumeUp_Click( object sender, RoutedEventArgs e ) {
        Volume = Math.Min( 1.0, Volume + 0.05 );
    }

    private void WasLoaded( object sender, EventArgs e ) {
        RepeatButton VolumeUpButton = (RepeatButton) Template.FindName( "PART_VolumeUp", this );
        if ( VolumeUpButton != null ) {
            VolumeUpButton.Click += VolumeUp_Click;
        }

        RepeatButton VolumeDnButton = (RepeatButton) Template.FindName( "PART_VolumeDown", this );
        if ( VolumeDnButton != null ) {
            VolumeDnButton.Click += VolumeDown_Click;
        }
    }
}

As I said, the Output window doesn't display any error messages, which is what WPF does when binding fail. Yet the value displayed by TextBlock that's bound to the dialog's Volume property doesn't change. Is the problem in the TextBlock binding, the Volume control's binding, or in the VolumeControl itself?

Tony


It appears that you have two Volume properties in play here. The first is on VolumeControl, which you show the code for. The other is on SettingsDialog, which you only show the XAML for.

Your TextBlock and VolumeControl are both bound to the Volume on your SettingsDialog, but it doesn't look like anything would update that property. You probably meant to use TwoWay binding on VolumeControl, like so:

<cs:VolumeControl ...
    Volume="{Binding Mode=TwoWay, Path=Volume, RelativeSource={RelativeSource AncestorType={x:Type c:SettingsDialog}}}" ... />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜