Silverlight 5绑定 - 为什么这不起作用?

时间:2012-08-24 16:31:37

标签: silverlight data-binding silverlight-5.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;
    }
}
}

1 个答案:

答案 0 :(得分:2)

您的绑定无效,因为:

  • 当您在ItemsSource中设置XAML时,它首先被执行并尝试绑定错误/空DataContext
  • 然后引发Loaded事件,该事件将设置正确的DataContext,但您现有的绑定将不会自动刷新。

如果必须在代码隐藏中设置DataContext,请在视图构造函数中执行此操作:

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

然后以下绑定应该有效:

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

WPF / Silverlight中的数据绑定需要公共属性。目前,您的viewmodel上的Frequency 是一个公共字段,请将其更改为属性,并且应该可以正常工作:

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

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