WPF使用属性绑定标签

时间:2016-08-31 08:49:02

标签: c# wpf xaml data-binding

在我的WPF应用页面上,我有2个绑定 1.作为可观察集合的项目列表 2.只是为了在标签上显示一些使用绑定的值

我的类结构

 public DbVersionModel DbVersion { get; set; }
  public ObservableCollection<BookStore> StoreCollection
  { get { return thisApp.app_Stores; } }


public class DbVersionModel : INotifyPropertyChanged
{
    private int _LocalVersion { get; set; }
    private int _ServerVersion { get; set; }
    private int _ActiveStores { get; set; }
    private string _LastModifiedLocal { get; set; }
    private string _LastModifiedServer { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyChange(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }

    public int LocalVersion
    {
        get { return _LocalVersion; }
        set
        {
            _LocalVersion = value;
            NotifyChange(new PropertyChangedEventArgs("LocalVersion"));
        }
    }

    public int ServerVersion
    {
        get { return _ServerVersion; }
        set
        {
            _ServerVersion = value;
            NotifyChange(new PropertyChangedEventArgs("ServerVersion"));
        }
    }

    public int ActiveStores
    {
        get { return _ActiveStores; }
        set
        {
            _ActiveStores = value;
            NotifyChange(new PropertyChangedEventArgs("ActiveStores"));
        }
    }

    public string LastModifiedLocal
    {
        get { return _LastModifiedLocal; }
        set
        {
            _LastModifiedLocal = value;
            NotifyChange(new PropertyChangedEventArgs("LastModifiedLocal"));
        }
    }

    public string LastModifiedServer
    {
        get { return _LastModifiedServer; }
        set
        {
            _LastModifiedServer = value;
            NotifyChange(new PropertyChangedEventArgs("LastModifiedServer"));
        }
    }
}   public ManagePage()
{
    InitializeComponent();
    setContext();
}
private void setContext()
{
     try
    {

       DbVersionModel db_version = new DbVersionModel();

        db_version.LastModifiedServer = //set with value;
        db_version.ServerVersion = //set with value;

        db_version.LocalVersion = //set with value;
        db_version.LastModifiedLocal = //set with value;

        db_version.ActiveStores = //set with value;
        this.DbVersion = db_version;

    }
    catch
    {

    }
}
像这样的xaml

<Page x:Class="App.ManagePage"
  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" 
  DataContext="{Binding RelativeSource={RelativeSource Self}}"
  mc:Ignorable="d">
<Page.Resources>

</Page.Resources>
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" >
   <Grid Margin="5 10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="150"></ColumnDefinition>
                            <ColumnDefinition ></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"></RowDefinition>
                            <RowDefinition Height="Auto"></RowDefinition>
                            <RowDefinition Height="Auto"></RowDefinition>

                            <RowDefinition Height="Auto"></RowDefinition>
                            <RowDefinition Height="Auto"></RowDefinition>
                            <RowDefinition Height="Auto"></RowDefinition>

                            <RowDefinition Height="Auto"></RowDefinition>
                        </Grid.RowDefinitions>
                        <!--//    -->
                        <Label Content="Active stores[Local]"  Grid.Column="0" Grid.Row="0" ></Label>
                        <Label Content="{Binding Source=DbVersion, Path=ActiveStores}"  Grid.Column="1" Grid.Row="0" ></Label>

                        <Label Content="Current version [Local]"  Grid.Column="0" Grid.Row="1" ></Label>
                        <Label Content="{Binding Source=DbVersion, Path=LocalVersion}"  Grid.Column="1" Grid.Row="1" ></Label>

                        <Label Content="Curretn Version [Server]"  Grid.Column="0" Grid.Row="2" ></Label>
                        <Label Content="{Binding Source=DbVersion, Path=ServerVersion}"  Grid.Column="1" Grid.Row="2" ></Label>

                        <Label Content="Last modified  [Local]"  Grid.Column="0" Grid.Row="3" ></Label>
                        <Label Content="{Binding Source=DbVersion, Path=LastModifiedLocal}"  Grid.Column="1" Grid.Row="3" ></Label>

                        <Label Content="Last  modified [Server]"  Grid.Column="0" Grid.Row="4" ></Label>
                        <Label Content="{Binding Source=DbVersion, Path=LastModifiedServer}"  Grid.Column="1" Grid.Row="4" ></Label>

                        <StackPanel   Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="6" HorizontalAlignment="Center">
                            <Button Name="btnSync" Content="Sync stores" Height="30" Click="btnSync_Click" Style="{StaticResource ActionButtons}"></Button>
                        </StackPanel>
                    </Grid>
    </ScrollViewer>

还有一个像这样加载的ItemsControl,绑定工作正常

    <ItemsControl Name="StoreListView" ItemsSource="{Binding StoreCollection}" Margin="5" VirtualizingStackPanel.IsVirtualizing="True"
                    VirtualizingStackPanel.VirtualizationMode="Recycling"  >

但ObservableCollection列表绑定没问题,但标签绑定不起作用。我怎么解决这个问题?

如何根据类

的属性在标签上设置绑定

1 个答案:

答案 0 :(得分:0)

我认为WPF没有注意到DbVersion属性的更改,因为您在调用InitializeComponent()后设置了它。要么将其实现为DependencyProperty,要么自动注意它是否已更改(请参阅:https://msdn.microsoft.com/en-us/library/ms750428(v=vs.110).aspx),或者使用INotifyPropertyChanged类中的ManagePage界面(请参阅: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx)。