数据绑定和INotifyPropertyChanged不起作用

时间:2011-02-25 10:17:05

标签: silverlight windows-phone-7 inotifypropertychanged

在我的WP7应用程序中,我希望在更改内容时同时更新所有页面上的ApplicationTitle字符串。我已经搜索了这样做的方法,人们谈论使用数据绑定和INotifyPropertyChanged接口。然而,使用我发现的样本,我无法使其工作。我怀疑绑定是错误的,但无法发现错误。

我有一个名为“RegistrationQueue”的类,其中包含以下代码:

    public void Gem()
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;

        if (settings.Contains("regqueue"))
        {
            settings["regqueue"] = _registreringsListe;
        }
        else
            settings.Add("regqueue", _registreringsListe);

        AppTitle = "LOGIMATIC A/S - " + _registreringsListe.Count + " registreringer i kø";
    }

而且:

    private string _AppTitle;

    // Declare the PropertyChanged event.
    public event PropertyChangedEventHandler PropertyChanged;

    // Create the property that will be the source of the binding.
    public string AppTitle
    {
        get { return _AppTitle; }
        set
        {
            _AppTitle = value;
            // Call NotifyPropertyChanged when the source property 
            // is updated.
            NotifyPropertyChanged("AppTitle");
        }
    }


    // NotifyPropertyChanged will raise the PropertyChanged event, 
    // passing the source property that is being updated.
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }

据我所知,这基本上是来自MSDN的复制/粘贴。

在我的一个XAML页面中,我写了这个:

<TextBlock x:Name="ApplicationTitle" Text="{Binding AppTitle, Mode=OneWay}" Style="{StaticResource PhoneTextNormalStyle}" />

我认为它会起作用,但事实并非如此。我在这里做错了什么?

页面的完整XAML:

<phone:PhoneApplicationPage 
    x:Class="FotoDokUdkast.loginScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
    shell:SystemTray.IsVisible="True" d:DesignHeight="696" d:DesignWidth="480">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Black">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <!--<TextBlock x:Name="ApplicationTitle" Text="LOGIMATIC A/S" Style="{StaticResource PhoneTextNormalStyle}"/>-->
            <TextBlock x:Name="ApplicationTitle" Text="{Binding Path=AppTitle, Mode=OneWay}" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="FotoDok 0.5b " Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Background="Black">
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="6,6,0,0" Name="textBlock1" Text="Et Logimatic program" VerticalAlignment="Top" Width="444" />
            <toolkit:ListPicker Name="pickerProject" Margin="9,42,15,0" Header="Vælg projekt" ListPickerMode="Normal" ItemCountThreshold="0" FullModeHeader="Vælg projekt" Height="97" VerticalAlignment="Top" />
            <Button Content="Log ind" Height="72" HorizontalAlignment="Left" Margin="6,337,0,0" Name="btnLogin" VerticalAlignment="Top" Width="444" Click="btnLogin_Click" />
            <toolkit:ListPicker Header="Vælg bruger" Margin="9,145,15,292" Name="pickerUser" ListPickerMode="Normal" ItemCountThreshold="0" FullModeHeader="Vælg bruger" />
        </Grid>
    </Grid>

    <!--Sample code showing usage of ApplicationBar-->
    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="Images/appbar.sync.rest.png" Text="Opdater projekter" Click="ApplicationBarIconButtonOpdater_Click"/>
            <shell:ApplicationBarIconButton IconUri="Images/appbar.delete.rest.png" Text="Slet projekter" Click="ApplicationBarIconButtonSlet_Click"/>
            <shell:ApplicationBarIconButton IconUri="Images/appbar.feature.email.rest.png" Text="Registreringer i kø" Click="ApplicationBarIconButtonRegistrering_Click"/>
            <shell:ApplicationBarIconButton IconUri="Images/appbar.feature.settings.rest.png" Text="Tilpas opsætning" Click="ApplicationBarIconButtonSettings_Click"/>

        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>

</phone:PhoneApplicationPage>

然后我在XAML页面的构造函数(loginscreen.cs)中写了“DataContext =”RegistreringsKø“;”

3 个答案:

答案 0 :(得分:2)

到目前为止,我发布的内容并没有出错。我的猜测是DataContext所在位置的TextBlock当前不是具有此AppTitle属性的类的实例。因此绑定不起作用,因为它找不到AppTitle属性。

答案 1 :(得分:1)

陈述显而易见的,但之前有点咬过我,你确定该类实现了INotifyPropertyChanged接口并且不只是声明了事件吗?

过去删除视图模型基类时,我一直被它咬了..花了我很多年才弄明白: - )

失败 - 调试时出现在“输出”窗口中的任何绑定错误?

答案 2 :(得分:0)

我没有正确使用Datacontext。 我写过ApplicationTitle.Text = "RegistreringsKø" 当然,这应该是对象的一个​​实例。

所以在我的情况下,DataContext = Registration.getInstance();解决了它。

相关问题