【问题标题】:Caliburn Micro Bindable collection - binding to combobox item source displays wrong textCaliburn Micro Bindable 集合 - 绑定到组合框项目源显示错误的文本
【发布时间】:2011-06-20 17:48:01
【问题描述】:

我有这个愚蠢的问题。我从 BindableCollection 的视图模型类属性类型绑定到 ComboBox 控件的 ItemSource 属性。

视图模型类的代码:

public class SpiritUser
{
    public string Nick { get; set; }

    public string Password { get; set; }
}


    public BindableCollection<SpiritUser> SpiritUsers
    {
        get { return _spiritUsers; }
        set
        {
            _spiritUsers = value;
            NotifyOfPropertyChange(() => SpiritUsers);
        }
    }


//constructor of view model class
        public LogOnViewModel()
        {
            SpiritUsers = new BindableCollection<SpiritUser>
                        {
                            new SpiritUser
                                {
                                    Nick = "Spirit_1",
                                    Password = "slniecko1"
                                },
                            new SpiritUser
                                {
                                    Nick = "Spirit_2",
                                    Password = "slniecko1"
                                }
                        };
        }

鉴于我有这个:

  Style on comboBox:

    <Style x:Key="LogOnView_NickComboBox" TargetType="{x:Type ComboBox}">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"></RowDefinition>
                        </Grid.RowDefinitions>
                        <Label Content="{Binding Path=Nick}" Grid.Column="0" Grid.Row="0"/>
                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Height" Value="25"/>
        <Setter Property="FontSize" Value="13"/>
        <Setter Property="Margin" Value="10,4,10,4"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

组合框控件:

<ComboBox ItemsSource="{Binding Path=SpiritUsers, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
          Style="{StaticResource LogOnView_NickComboBox}"
          SelectedValuePath="Nick"
          Text="{Binding Path=Nick, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
          SelectedValue="{Binding Path=Nick, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          IsEditable="True"/>

如果我选择某个组合框项目,我会看到 Spirit.Models.SpiritUser 而不是项目文本。

如果组合框属性 IsEditable 设置为 true,则会出现问题。

我该如何解决这个问题,我需要从组合框上的视图模型绑定属性,但我还需要组合框可编辑并将用户输入绑定到视图模型中的属性。

【问题讨论】:

    标签: wpf binding combobox caliburn.micro


    【解决方案1】:

    如果是可编辑的组合框,请使用 DisplayMemberPath 属性而不是 ItemTemplate 来指定要显示的绑定对象的属性:

    <ComboBox ItemsSource="{Binding Path=SpiritUsers, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
              Style="{StaticResource LogOnView_NickComboBox}"
              DisplayMemberPath="Nick"
              SelectedValuePath="Nick"
              Text="{Binding Path=CurrentUserNick, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
              IsEditable="True"/>
    

    如果您仍然想使用 ItemTemplate,那么您可以通过 TextSearch.TextPath 附加属性指定您的对象的哪些属性应显示在文本框中:

    <ComboBox ItemsSource="{Binding Path=SpiritUsers, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
              Style="{StaticResource LogOnView_NickComboBox}"
              SelectedValuePath="Nick"
              TextSearch.TextPath="Nick"
              Text="{Binding Path=CurrentUserNick, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
              IsEditable="True"/>
    

    【讨论】:

    • 感谢 Glazkov 先生,第一个解决方案非常优雅 :)
    猜你喜欢
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    相关资源
    最近更新 更多