runtime绑定到用户控件的datacontext时的异常

时间:2013-04-28 08:17:16

标签: wpf xaml exception user-controls datacontext

这是主页的XAML。

<StackPanel x:Name="menuStackPanel" Grid.Row="4" Grid.ColumnSpan="2"
    Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">

    <StackPanel.DataContext>
        <Model:GameSettings/>
    </StackPanel.DataContext>

    <uilibrary:CustomImageButton x:Name="btnPlay" Scale="1.5" 
        ReadyImage="/App;component/Images/Menus/menu_play.png" 
        SelectedImage="/App;component/Images/Menus/menu_play_selected.png" Margin="5"/>

    <uilibrary:CustomImageButton x:Name="btnPlanet" StickyState="True" Scale="1.5" 
         ReadyImage="/App;component/Images/Menus/menu_planet.png" 
         SelectedImage="/App;component/Images/Menus/menu_planet_selected.png" 
         AIsSelected="{Binding Path=UsePlanet, Mode=TwoWay, ElementName=menuStackPanel}" 
         Margin="5">                    
    </uilibrary:CustomImageButton>

    <uilibrary:CustomImageButton x:Name="btnGravity" StickyState="True" Scale="1.5" 
        ReadyImage="/App;component/Images/Menus/menu_gravity.png"
        SelectedImage="/App;component/Images/Menus/menu_gravity_selected.png" 
        AIsSelected="True" Margin="5">
    </uilibrary:CustomImageButton>
</StackPanel>

以下是UserControl'CustomImageButon'

的XAML
<UserControl x:Class="UILibrary.CustomImageButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">

     <Grid x:Name="LayoutRoot" Background="Transparent">
        <Image x:Name="MyImage" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center">
            <Image.RenderTransform>
                <CompositeTransform x:Name="buttonTransform"/>
            </Image.RenderTransform>
        </Image>
     </Grid>
</UserControl>

以下是UsePlanet属性的代码,'MyUsePlanet'的值默认为false。

public bool UsePlanet
{
    get
    {
        return MyUsePlanet;
    }

    set
    {
        MyUsePlanet = value;
        IsolatedStorageSettings.ApplicationSettings[GameSettings.key_planet] = value;
        IsolatedStorageSettings.ApplicationSettings.Save();

        this.OnPropertyChanged("UsePlanet");
    }
}

这是我的例外信息

A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
'UI Task' (Managed): Loaded 'System.SR.dll'
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'MS.Internal.NativeParseException' occurred in System.Windows.dll
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.dll
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.dll

Additional information: Set property 'UILibrary.CustomImageButton.AIsSelected' threw an exception. [Line: 95 Position: 46]

我知道它与AIsSelected="{Binding Path=UsePlanet, Mode=TwoWay, ElementName=menuStackPanel}"有关,因为如果我将它设置为“True”,那么问题就会消失,但我希望它能绑定到我的模型。此类其他绑定也可以正常使用。

<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Text="{Binding ShipAScore}">
    <TextBlock.DataContext>
        <Model:Scores/>
    </TextBlock.DataContext>
</TextBlock>

UDATE 4/28/2013 在我的UserControl后面的代码中添加依赖属性解决了这个问题。

public static readonly DependencyProperty AIsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(CustomImageButton), new PropertyMetadata(false));

0 个答案:

没有答案