繁体   English   中英

WPF数据绑定到Window DataContext对象属性

[英]WPF data binding to Window DataContext object properties

** [已解决] **这是我在这里提出的第一个问题,因此可能会出现一些错误。 在我的WPF应用程序中,我想以“双向模式”将所有控件绑定到特殊对象实例BuildingManagementSystem相应属性,我想将其设置为Window DataContext。 但是没有任何效果(不显示数据)。 绑定它的正确方法是什么?

这是我的代码:

public partial class MainWindow : Window
{

    BuildingManagementSystem bms { get; set; }

    public MainWindow()
    {    
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Window1_Loaded);
    }

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        bms = new BuildingManagementSystem();
        this.DataContext = bms;         
}

public class BuildingManagementSystem
{
    public string Name { get; set; }

    public readonly FireAlarmSystem FireAlarmSystem;

    public BuildingManagementSystem()
    {          
        FireAlarmSystem = new FireAlarmSystem();         
    }
}

class FireAlarmSystem
{

    private int alarmSmokeRate, currentSmokeRate;

    public  List<PowerConsumer> Devices { get; set; }

    public int CurrentSmokeRate
    {
        get { return currentSmokeRate; }
        set { SetField(ref currentSmokeRate, value, () => CurrentSmokeRate); }
    }


    public FireAlarmSystem()
    {
        Devices = new List<PowerConsumer>();

    }
}

class PowerConsumer
{
    public string Name { get; set; }
    public double Power { get; set; }
    public int Priority { get; set; }
    public bool Enabled { get; set; }
}

XAML:

<DataGrid Name="FireAlarmGrid" HorizontalAlignment="Left" Margin="10,51,0,0" CanUserAddRows="True" 
            CanUserDeleteRows="True" VerticalAlignment="Top" AutoGenerateColumns="False" ItemsSource="{Binding  FireAlarmSystem.Devices}"  >

    <DataGrid.RowValidationRules>

        <local:FireValidationRule ValidationStep="UpdatedValue"/>

    </DataGrid.RowValidationRules>

    <DataGrid.Columns>
        <DataGridCheckBoxColumn Header="Enabled" Binding="{Binding Enabled}"></DataGridCheckBoxColumn>
        <DataGridTextColumn Header="Name" Binding="{Binding Name,ValidatesOnExceptions=True }" >

        </DataGridTextColumn>
        <DataGridTextColumn Header="Power" Binding="{Binding Power}"></DataGridTextColumn>

    </DataGrid.Columns>

</DataGrid>



<TextBox Name="tbCurrentSmokeRate"   Text="{Binding Path=FireAlarmSystem.CurrentSmokeRate, Mode=TwoWay}" VerticalAlignment="Top" Width="70"/>  

我个人喜欢创建viewmodel的静态实例,如下所示:

<Window.Resources>
    <ViewModels:MainWindowVM x:Key="VM"></ViewModels:MainWindowVM>

当我这样做时,绑定很容易,因为属性页面会在VS中为您找到属性。

 <StackPanel Grid.Column="0" Margin="5,5,5,0"  
          DataContext="{Binding Source={StaticResource VM}}">

但是请记住,您需要添加名称空间

 xmlns:ViewModels="clr-namespace:MyWPF.ViewModels" 
 xmlns:Views="clr-namespace:MyWPF.Views" 

这样允许属性绑定如下:

    <Views:UcTitle x:Name="XTitle" ></Views:UcTitle>
    <Views:UcLegendTitle x:Name="XLegendTitle"/>
    <Views:UcSeriesTitle x:Name="XSeriesTitle" />
    <Views:UcSeriesTypes x:Name="XSeriesTypes"/>

而且您不必在其中键入任何名称。在您的情况下,您没有使用ViewModel,而是在正确设置数据上下文。 因此,这只能是无数据显示或属性绑定不正确。 请记住,要使此功能起作用,您需要三件事:1)DataContext 2)Vales和3)按名称正确绑定...这是刚开始使用强大的WPF绑定系统时引起很多人关注的第三点。

要使MVVM工作...

您将需要更新您的视图模型以实现INotifyPropertyChanged,以使WPF知道您的属性已更改

使用ObservableCollections而不是列表,ObservableCollections具有让Datagrid listview的连线,列表框知道集合中的项已更改

将字段更改为属性,以便WPF可以看到它...

<DataGrid Name="FireAlarmGrid" HorizontalAlignment="Left" Margin="10,51,0,0" CanUserAddRows="True" 
                          CanUserDeleteRows="True" VerticalAlignment="Top" AutoGenerateColumns="False" ItemsSource="{Binding  FireAlarmSystem.Devices}"  >

        <DataGrid.RowValidationRules>

            <local:FireValidationRule ValidationStep="UpdatedValue"/>

        </DataGrid.RowValidationRules>

        <DataGrid.Columns>
            <DataGridCheckBoxColumn Header="Enabled" Binding="{Binding Enabled,Mode=TwoWay}"></DataGridCheckBoxColumn>
            <DataGridTextColumn Header="Name" Binding="{Binding Name,ValidatesOnExceptions=True,Mode=TwoWay }"  >

            </DataGridTextColumn>
            <DataGridTextColumn Header="Power" Binding="{Binding Power,Mode=TwoWay}"></DataGridTextColumn>

        </DataGrid.Columns>
    </DataGrid>

    <TextBox Name="tbCurrentSmokeRate"   Text="{Binding Path=FireAlarmSystem.CurrentSmokeRate, Mode=TwoWay}" VerticalAlignment="Top" Width="70"/>


public class BuildingManagementSystem : INotifyPropertyChanged
{
    private string _Name;

    public string Name
    {
        get { return _Name; }
        set
        {
            if (_Name != value)
            {
                _Name = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Name"));
            }
        }
    }

    private FireAlarmSystem _fireAlarmSystem;
    public FireAlarmSystem FireAlarmSystem { get { return _fireAlarmSystem; } }

    public BuildingManagementSystem()
    {
        _fireAlarmSystem = new FireAlarmSystem();
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    #endregion
}

public class FireAlarmSystem : INotifyPropertyChanged
{

    private int alarmSmokeRate, currentSmokeRate;

    public ObservableCollection<PowerConsumer> Devices { get; set; }


    public int CurrentSmokeRate
    {
        get { return currentSmokeRate; }
        set
        {

            //SetField(ref currentSmokeRate, value, () => CurrentSmokeRate);
            PropertyChanged(this, new PropertyChangedEventArgs("CurrentSmokeRate"));
        }
    }


    public FireAlarmSystem()
    {
        Devices = new ObservableCollection<PowerConsumer>();
        //Create some test data...
        Devices.Add(new PowerConsumer() { Name = "One", Enabled = true, Power = 100, Priority = 1 });
        Devices.Add(new PowerConsumer() { Name = "two", Enabled = false, Power = 101, Priority = 2 });
        Devices.Add(new PowerConsumer() { Name = "three", Enabled = false, Power = 103, Priority = 3 });
    }
    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    #endregion
}

public class PowerConsumer
{
    public string Name { get; set; }
    public double Power { get; set; }
    public int Priority { get; set; }
    public bool Enabled { get; set; }
}

这里有几个错误

  1. 似乎BuildingManagementSystem旨在成为您的数据上下文

代替BuildingManagementSystem bms {get; 组; 编写以下代码:

BuildingManagementSystem bms = new BuildingManagementSystem();

  1. 您需要在ViewModels中实现INotifyPropertyChanged事件,以反映UI上的更改
  2. 如果要为每个基础子视图模型分配值,请使用参数化构造函数来传递和分配值。 仅在父级中实例化子级viewModels并没有任何作用。

归功于所有,我发现了一个错误,那就是将FireAlarmSystem定义为readonly ,它应该是一个属性。 2 Stuart Smith,您使用INotifyPropertyChanged是正确的,但是我有一个抽象的class-ancestor BuildingSystem ,它实现了此接口。 我忘记发布了。

暂无
暂无

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

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