WPF 绑定问题

WPF Binging problems

我有一个用户控件,它的数据上下文绑定到 "SelectedSchedule",单击按钮打开 window,可以编辑 "SelectedSchedule",工作正常。在这个 window 中有一个组合框,其中有一些 "SelectedSchedule" 可供选择,其中 SelectedItem-属性 绑定到 "SelectedSchedule"。当我现在在组合框中选择另一个对象时,它没有得到新对象,什么都没有 happen/change.

我做错了什么?

用户控制-XAML:

<Label Content="{Binding Path=SelectedSchedule.Name}" Margin="0,-6,0,0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" Height="32" Width="87"/>

用户控制视图模型

    private Schedule mSelectedSchedule;

    public Schedule SelectedSchedule
    {
        get { return mSelectedSchedule; }
        set
        {
            mSelectedSchedule = value;
            OnPropertyChanged("SelectedSchedule");
        }
    }

    public EmployeeWeekCheckButon_VM(Schedule GivenSchedule)
    {
        SelectedSchedule = GivenSchedule;
    }

    private void Edit()
    {
        Forms.Tracking.View.frmEditTracking newForm = new Forms.Tracking.View.frmEditTracking(SelectedSchedule);
        newForm.ShowDialog();
        OnPropertyChanged("SelectedSchedule");
    }

    private void Delete()
    {
        SelectedSchedule = null;
    }

编辑-Window-XAML:

<ComboBox ItemsSource="{Binding ListOfSchedule}" SelectedItem="{Binding SelectedSchedule}" x:Name="cmdSchedule" HorizontalAlignment="Left" FontSize="16" Margin="17,27,0,0" VerticalAlignment="Top" Width="120"/>

编辑-Window-ViewModel:

    private Schedule _SelectedSchedule;

    public Schedule SelectedSchedule
    {
        get { return _SelectedSchedule; }
        set { _SelectedSchedule = value;
            OnPropertyChanged("SelectedSchedule"); }
    }

    private ObservableCollection<object> _ListOfSchedule;

    public ObservableCollection<object> ListOfSchedule
    {
        get { return _ListOfSchedule; }
        set { _ListOfSchedule = value;
            OnPropertyChanged("ListOfSchedule");
        }
    }

    public frmEditTracking_VM(Schedule GivenSchedule)
    {
        SelectedSchedule = GivenSchedule;
    }

    private void SaveAndClose()
    {
        SelectedSchedule.isTracked = true;
        OnClosingRequest();
    }

尝试在two-way模式下设置绑定

SelectedItem="{Binding SelectedSchedule, Mode=TwoWay}"

并且当对话框关闭时,您需要设置新值,因为对话框和视图模型之间没有 link "SelectedSchedule" 属性

newForm.ShowDialog();
SelectedSchedule = newForm.SelectedSchedule;

要了解为什么版本有效但分配无效,您可以尝试:

private Schedule mSelectedSchedule2;

public Schedule SelectedSchedule2
{
    get { return mSelectedSchedule2; }
    set
    {
        mSelectedSchedule2 = value;
        OnPropertyChanged("SelectedSchedule2");
    }
}

private Schedule _SelectedSchedule;

public Schedule SelectedSchedule
{
    get { return _SelectedSchedule; }
    set { _SelectedSchedule = value;
        OnPropertyChanged("SelectedSchedule"); }
}



 public EmployeeWeekCheckButon_VM(Schedule GivenSchedule)
 {
    SelectedSchedule = GivenSchedule;
    SelectedSchedule2 = GivenSchedule;
    SelectedSchedule.Name = "Test";
    Debug.WriteLine(SelectedSchedule.Name) //it's Test
    Debug.WriteLine(SelectedSchedule2.Name) //it's Test
    SelectedSchedule = new Schedule();
    SelectedSchedule.Name = "Test2";
    Debug.WriteLine(SelectedSchedule.Name) //it's Test2
    Debug.WriteLine(SelectedSchedule2.Name) //it's still Test because 
    //it's referencing the first object

 }