0

我可以像这样在代码隐藏中绑定一个组合框:

    private void comboBox1_Loaded(object sender, RoutedEventArgs e)
    {
        var combo = sender as ComboBox;
        App.SchedulerVM = new ScheduleViewModel();
        combo.DataContext = App.SchedulerVM;
        combo.ItemsSource = App.SchedulerVM.Frequency;
    }

这有效 - 我的组合框包含 SchedulerVM 对象中频率列表中的项目。

但是,我不想在代码隐藏中做任何这些。但是我之前在 WP7 中这样做的方法在这里不起作用。如果我在上面的 Loaded 方法中注释掉最后一行并尝试在 XAML 中设置 ItemsSource,它不起作用 - 没有任何显示:

<ComboBox Name="comboBox1" Loaded ="comboBox1_Loaded" ItemsSource="{Binding 
Frequency}" />

这也不起作用:

<ComboBox Name="comboBox1" Loaded ="comboBox1_Loaded" ItemsSource="{Binding 
App.SchedulerVM.Frequency}" />

也不是这个:

<ComboBox Name="comboBox1" Loaded ="comboBox1_Loaded" ItemsSource="{Binding 
SchedulerVM.Frequency}" />

理想情况下,DataContext 也不必在此控件的代码隐藏中显式设置,它将从 LayoutRoot 继承,我已在代码隐藏中设置它。但这是我在这里进行故障排除的第 2 步。

我究竟做错了什么?'

谢谢!

编辑 ScheduleViewModel 如下所示:

namespace SchedulerUI.ViewModels
{
public class ScheduleViewModel : INotifyPropertyChanged
{
    //private properties
    private Schedule _thisSchedule; 

    //public properties
    public Schedule ThisSchedule
    {
        get { return _thisSchedule; }
        set
        {
            if (value != _thisSchedule)
            {
                NotifyPropertyChanged("ThisSchedule");
            }

            _thisSchedule = value;
        }
    }

    public List<string> Frequency = new List<string>();
    public string Test;

    //constructors
    public ScheduleViewModel()
    {
        Frequency.AddRange(new string[] { "Daily", "Weekly", "Monthly" });
        Test = "This is only a test.";
    }

    //INotifyPropertyChanged Implementation
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
}

这是整个 XAML:

<UserControl x:Class="SchedulerUI.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">

        <ComboBox Height="23" HorizontalAlignment="Left" Margin="34,41,0,0" Name="comboBox1" Loaded ="comboBox1_Loaded" VerticalAlignment="Top" Width="120" ItemsSource="{Binding Frequency}" />
        <TextBox BorderBrush="Black"  HorizontalAlignment="Left" Margin="34,41,0,0" Width="100" Height="100" DataContext="LayoutRoot.DataContext"  Text="{Binding  Test}" /> 

</Grid>
</UserControl>

这是整个代码隐藏:

namespace SchedulerUI
{
public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        App.SchedulerVM = new ScheduleViewModel();
        comboBox1.DataContext = App.SchedulerVM;
        List<string> testlist = App.SchedulerVM.Frequency;
        string teststring = App.SchedulerVM.Test;
    }

    private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
    {
        //App.SchedulerVM = new ScheduleViewModel();
        //var root = sender as Grid;
        //if (root != null)
        //{
        //    root.DataContext = App.SchedulerVM;
        //}
    }

    private void comboBox1_Loaded(object sender, RoutedEventArgs e)
    {
        //var combo = sender as ComboBox;
        //App.SchedulerVM = new ScheduleViewModel();
        //combo.DataContext = App.SchedulerVM;
        //combo.ItemsSource = App.SchedulerVM.Frequency;
    }
}
}
4

1 回答 1

2

您的绑定不起作用,因为:

  • 当您设置ItemsSourceXAML首先执行并尝试绑定错误/空DataContext
  • 然后Loaded引发事件,这将设置正确DataContext但您已经存在的绑定不会自动刷新。

如果您必须DataContext在代码隐藏中设置,请在您的视图构造函数中进行:

public YourView()
{
    InitializeComponent();
    combo.DataContext = App.SchedulerVM;
}

然后以下绑定应该起作用:

<ComboBox Name="comboBox1" ItemsSource="{Binding Frequency}" />

WPF/Silverlight 中的数据绑定需要 公共属性。当前Frequency 是您的视图模型上的一个公共字段,将其更改为一个属性,并且一切都应该工作:

private List<string> frequency = new List<string>();
public List<string> Frequency { get { return frequency; } set { frequency = value; }

这就是为什么它在你的初始加载事件中起作用的原因,因为你没有在那里使用数据绑定,但你只是设置了combo.ItemsSource.

于 2012-08-24T17:07:50.960 回答