WPF Binging的问题

时间:2018-03-18 19:23:14

标签: wpf data-binding binding wpf-controls

我有一个usercontrol,datacontext绑定到" SelectedSchedule",点击一个按钮,一个窗口打开,其中" SelectedSchedule"可以编辑,工作正常。在这个窗口中有一个组合框,其中有一些" SelectedSchedule"选择,哪个SelectedItem-Property绑定到" SelectedSchedule"。当我现在在组合框中选择另一个对象时,它没有得到新对象,只是没有发生/改变。

我做错了什么?

用户 - 控制 - 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;
    }

编辑视窗-XAML:

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

编辑视窗 - 视图模型:

    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();
    }

2 个答案:

答案 0 :(得分:1)

尝试以双向模式设置绑定

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

当对话框关闭时,您需要设置新值,因为对话框和viewmodel“SelectedSchedule”属性之间没有链接

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

答案 1 :(得分:0)

要了解版本的工作原理,但是您可以尝试分配:

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

 }