ComboBox所选项目未更新

时间:2016-10-05 11:14:04

标签: c# wpf data-binding

问题

我正在尝试将ComboBox的SelectedItem绑定到自定义类,但在更改属性时不会更新。INotifyPropertyChanged已实现。

DataContext

DataContext是一个包含许多属性的自定义类,但下面提到了这一点。你可以看到它实现了INotifyPropertyChanged,当两个属性发生变化时就会调用它。

public class BctsChange : INotifyPropertyChanged
{
    #region declarations
    private byContact _Engineer;

    public byContact Engineer
    {
        get { return _Engineer; }
        set
        {
            _Engineer = value;
            NotifyPropertyChanged("Engineer");
            OnEngineerChanged();
        }
    }

    private BctsSvc.DOSets _LeadingSet;

    public BctsSvc.DOSets LeadingSet
    {
        get { return _LeadingSet; }
        set { _LeadingSet = value; NotifyPropertyChanged("LeadingSet"); }
    }
    #endregion

    #region INotify
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion

    public BctsChange()
    {
        Engineer = new byContact(Environment.UserName);
    }

    private void OnEngineerChanged()
    {
        if (Engineer != null)
        {
            BctsSvc.DOSets leadSet = GetLeadingSetFromDeptCode(Engineer.DeptCode);

            if (leadSet == null) return;
            LeadingSet = leadSet;
        }
    }


    private static BctsSvc.DOSets GetLeadingSetFromDeptCode(string DeptCode)
    {
        BctsSvc.BctsServiceSoapClient svc = new BctsSvc.BctsServiceSoapClient();
        BctsSvc.DOSets setX = svc.GetSetFromDeptCode(DeptCode);
        return setX;
    }
}

Window XAML

我在窗口上有几个控件,但为了保持代码简单,我相信以下提取就足够了。

<Window x:Class="MyNamespace.wdSubmit"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:my="clr-namespace:MyNamespace"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            x:Name="ucReqForm"
        Title="wdSubmit" >
    <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch">        
        <GroupBox Header="Engineer Details" Name="grpOwnerDetails" >
            <StackPanel Orientation="Vertical">
                <Grid VerticalAlignment="Top">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="35"/>
                    </Grid.ColumnDefinitions>
                    <Label Content="{Binding Engineer.FullName,  FallbackValue='Please select an engineer by clicking →', Mode=OneWay}" Margin="5,0" IsEnabled="True" FontStyle="Italic" />
                    <Button Content="{StaticResource icoSearch}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Column="1" Height="23" Name="btnSelectEngineer" Margin="0,0,5,0" HorizontalAlignment="Stretch" ToolTip="Search for an engineer responsible" Click="btnSelectEngineer_Click" />
                </Grid>

                <ComboBox Height="23" x:Name="ddSet2" Margin="5,0" ItemsSource="{Binding LeadingSets, Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" SelectedItem="{Binding LeadingSet, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}" >
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding SetName}" ToolTip="{Binding HelpInfo}"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
                <my:LabelledDropdown Height="23" x:Name="ddSet" Margin="5,0" ItemsSource="{Binding LeadingSets, Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" SelectedItem="{Binding LeadingSet, Mode=TwoWay,NotifyOnTargetUpdated=True,NotifyOnSourceUpdated=True}" Label="e.g. BodyHardware">
                    <my:LabelledDropdown.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding SetName}" ToolTip="{Binding HelpInfo}"/>
                        </DataTemplate>
                    </my:LabelledDropdown.ItemTemplate>
                </my:LabelledDropdown>
            </StackPanel>
        </GroupBox>
    </StackPanel>
</Window>

上述摘录包含:

  • 包含联系人姓名的Label和搜索联系人的按钮,该按钮绑定到FullName的{​​{1}}
  • 包含公司内部部门的Engineer,绑定到ComboBox,其中包含部门列表
  • 两个ObservableCollection<DOSets> es,一个是自定义的,另一个是临时的,以确保错误不在控件内。这些是数据绑定到ComboBox

背后的窗口代码

在后面的代码中,我将DataContext设置为LeadingSet。当用户想要选择其他CurrentChange时,这将更新Engineer中工程师的所选部门。

当用户更改工程师时,工程师的数据绑定会更新,但所选部门(领导集)不会更新。

CurrentChange

1 个答案:

答案 0 :(得分:0)

我意识到问题可能是由LeadingSet引起的,当工程师被更改时返回,与ObservableCollection中的实例不同。