【问题标题】:WPF Binding a label with propertiesWPF将标签与属性绑定
【发布时间】:2017-01-07 18:24:19
【问题描述】:

在我的 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 列表绑定很好,但是标签绑定不起作用。我该如何解决这个问题?

如何根据类的属性设置标签绑定

【问题讨论】:

    标签: c# wpf xaml data-binding


    【解决方案1】:

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

    【讨论】:

    • 或者在ManagePage构造函数中在InitializeComponent();之前调用setContext();
    • 我更改了我的模型,InotifyProperty changd 事件仍未显示
    • 您还需要在您的ManagePage 类上实现INotifyPropertyChanged。在您的实现中,只有 DbVersionModel 通知其属性之一是否发生更改。如果DbVersion 属性发生变化,您必须通知ManagePage 类。
    • 或者在这里使用DependencyProperty 代替DbVersion
    猜你喜欢
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 2014-07-30
    • 2018-10-24
    • 2011-06-22
    • 1970-01-01
    相关资源
    最近更新 更多