English 中文(简体)
6.Billy MVVER Combo Box 破碎
原标题:Silverlight MVVM Combobox Binding getting broken

我有这样定义的 com子。

<ComboBox Name="RoomDropDown" Visibility="{Binding Path=RoomDropDownVisible,Mode=OneWay,Converter={StaticResource BoolVisibilityConvertor}}"
                          ItemsSource="{Binding Path=RoomList,Mode=OneWay}" DisplayMemberPath="display" SelectedValuePath="display" SelectedValue="{Binding Path=Room,Mode=TwoWay}"/>

《观点》界定了2种财产,即名单上的文官,以及房屋财产。

第一次,当所有东西都奏效时,Dop Down获得正确的价值,选择正确的价值。 然而,在某些条件下,房东的财产被改变为不同的来源和营地;会议室财产也发生了变化。 正在发生的问题是,Combo Box正在显示正确的价值,但选定的价值并未被选定。 更糟糕的是,当DroopDown的数值有手工改变时,我们就能够生活在这一环境中。

这里的哪一点是错的?

Followup: Don t think I managed to get the exact problem across, here is some sample code that I wanted to add to illustrate the problem:

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel VerticalAlignment="Center" Width="100">
        <ComboBox Name="TestBox" Height="20" Width="100" ItemsSource="{Binding Path=ComboSource}" DisplayMemberPath="display" SelectedValuePath="code" 
                  SelectedValue="{Binding Path=ComboSelection,Mode=TwoWay}"/>
        <Button Content="Click Here" Click="Button_Click" />
    </StackPanel>
 </Grid>

法典:

public MainPage()
    {
        InitializeComponent();
        this.Loaded += (s, e) =>
            {
                var temp = new List<Binding>();
                temp.Add(new Binding() { code = "1", display = "One" });
                temp.Add(new Binding() { code = "2", display = "Two" });
                this.ComboSource = temp;
                this.ComboSelection = "1";
                this.DataContext = this;
            };
    }

    private static readonly DependencyProperty ComboSelectionProperty =
        DependencyProperty.Register("ComboSelectionProperty", typeof(string), typeof(MainPage), new PropertyMetadata(null));

    public string ComboSelection
    {
        get { return (string)GetValue(ComboSelectionProperty); }
        set 
        { 
            SetValue(ComboSelectionProperty, value);
            this.RaisePropertyChanged("ComboSelection");
        }
    }

    private static readonly DependencyProperty ComboSourceProperty =
        DependencyProperty.Register("ComboSourceProperty", typeof(List<Binding>), typeof(MainPage), new PropertyMetadata(null));

    public List<Binding> ComboSource
    {
        get
        {
            return (List<Binding>)GetValue(ComboSourceProperty);
        }
        set
        {
            SetValue(ComboSourceProperty, value);
            this.RaisePropertyChanged("ComboSource");
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var temp = new List<Binding>();
        temp.Add(new Binding() { code = "3", display = "Three" });
        temp.Add(new Binding() { code = "4", display = "Four" });

        this.ComboSource = temp;
        this.ComboSelection = "3";

    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
 }

 public class Binding
{
    public string code {get; set;}
    public string display { get; set; }
}

不仅严格来说,为了解释问题,当纽托尼点击事件发生时,博迪来源也发生了变化,正在作出新的选择,但甄选没有约束力,上述问题开始出现。

最佳回答

不知道您的实际来源代码,但您的扶养登记并不正确。

应:

DependencyProperty.Register("ComboSelection"

以及

DependencyProperty.Register("ComboSource"

不包括“xxxProperty”。 然后,所有变化都是正确的。

基本上,你正在登记依赖物,而不是取用/设定财产方法。 参数文件/名称似乎没有什么误导。

*Note: As per Dan Bryant s comment - the INotifyPropertyChange stuff is also not needed 以及 I ripped it out of your sample code (I can assure you it works perfectly fine without it).

问题回答

暂无回答




相关问题
WPF Datagrid, Setting the background of combox popup

I would like to change the color of the popup background when using a DatagridComboboxColumn in the WPF Toolkit datagrid. I ve edited the Template for a normal Combobox and it works great for selected ...

How to insert ComboBox item into ListBox? [winforms]

The question is very simple, How to insert ComboBox selected item into ListBox using c#? I have tried with this: listbox.Items.Add(combobox.SelectedItem); and some other permutations but it always ...

How do I bind a ComboBox to a one column list

I ve seen how to bind a ComboBox to a list that has columns like this: ItemsSource="{Binding Path=Entries}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=Entry}" But ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

热门标签