简体   繁体   English

WPF将标签与属性绑定

[英]WPF Binding a label with properties

On my WPF app page i have 2 bindings 1. for a list of items as an observable collection 2. just to show some values on a lable using binding 在我的WPF应用程序页面上,我有2个绑定1.用于作为可观察集合的项目列表2.使用绑定在标签上显示一些值

My class struct like this 我的课堂结构是这样的

 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 like this 像这样的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>

Also have a ItemsControl which is getting loaded like this and binding is working fine here 也有一个ItemsControl正在像这样加载,并且绑定在这里工作正常

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

But ObservableCollection list binding is fine , but the label bindings are not working. 但是ObservableCollection列表绑定很好,但是标签绑定不起作用。 How i can solve this? 我该如何解决?

How can i set binding on label based on properties of a class 如何基于类的属性在标签上设置绑定

I think WPF does not notice the change of the DbVersion property because you set it after calling InitializeComponent() . 我认为WPF不会注意到DbVersion属性的更改,因为您在调用InitializeComponent()之后进行了设置。 Either you implement it as a DependencyProperty , which automatically notices if it is changed (see: https://msdn.microsoft.com/en-us/library/ms750428(v=vs.110).aspx ), or you use the INotifyPropertyChanged interface in your ManagePage class (see: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx ). 您可以将其实现为DependencyProperty ,它会自动注意到它是否已更改(请参阅: https : //msdn.microsoft.com/zh-cn/library/ms750428 ( v=vs.110 ) .aspx ),或者您可以使用您的ManagePage类中的INotifyPropertyChanged接口(请参阅: https : ManagePage ( v= ManagePage )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM