silverlight - 数据绑定(bind)和 INotifyPropertyChanged 不起作用

标签 silverlight windows-phone-7 inotifypropertychanged

在我的 WP7 应用程序中,当某些内容发生更改时,我想同时更新所有页面上的 ApplicationTitle 字符串。我已经搜索过这样做的方法,人们谈论使用数据绑定(bind)和 INotifyPropertyChanged 接口(interface)。但是,使用我找到的示例,我无法使其工作。我怀疑绑定(bind)有误,但未能发现错误。

我有一个名为“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ø";”

最佳答案

到目前为止,我看不出您发布的内容有任何问题。我的猜测是 TextBlock 所在位置的 DataContext 当前不是具有此 AppTitle 属性的类的实例。因此绑定(bind)不起作用不是因为它找不到 AppTitle 属性。

关于silverlight - 数据绑定(bind)和 INotifyPropertyChanged 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5116021/

相关文章:

C#复制构造函数生成器

javascript - 确定 Silverlight 或 Javascript 中 ActiveX 控件的版本

c# - 如何将 ListBox 所选项目背景更改为图像?

silverlight - Windows系列电话7 "Cloud"

c# - Hook XmlSerializer 的 OnDeserializing

silverlight - 在Silverlight中单击HyperlinkBut​​ton时,如何避免出现偏蓝的边框?

Silverlight 可写位图

c# - 实现 INotifyPropertyChanged 时属性 Getters 和 Setters?

c# - 使 VirtualizingStackPanel 不清理已加载的项目

WPF Observablecollection 与 ListView 的 INotifyPropertyChanged 不起作用