【问题标题】:Listbox binding get selected item value列表框绑定获取选定项的值
【发布时间】:2016-12-13 09:55:41
【问题描述】:

我是 Wpf 的新手,我遇到了在列表框中获取所选项目的问题 我创建了一个带有列表框和文本框的简单 xaml。 我使用绑定来填充我的列表框,包括我想稍后使用的触发器(选中与否)。

Xaml 代码:

<ListBox x:Name="LstB_Checklist" HorizontalAlignment="Left" Height="190" Margin="39,45,0,0" VerticalAlignment="Top" Width="275" Background="#FF363636" BorderBrush="{x:Null}" FontSize="18" Foreground="White" BorderThickness="2" SelectionChanged="LstB_Checklist_SelectionChanged" SelectedItem="{Binding SelectedProperty,Mode=TwoWay}" >
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True" >
                        <Setter Property="FontWeight" Value="Bold" />
                        <Setter Property="Background" Value="#FFFFDC00" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                </Style.Triggers>
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGray"/>
                </Style.Resources>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image>
                        <Image.Style>
                            <Style TargetType="{x:Type Image}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Checked}" Value="false">

                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Checked}" Value="true">

                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>

                    <TextBlock Text="{Binding  Path=Title, Mode=TwoWay}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <TextBox x:Name="txtb_Selection" HorizontalAlignment="Left" Height="23" Margin="60,264,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>

在后面的代码中

public MainWindow()
    {
        InitializeComponent();
        List<LstB_Item> items = new List<LstB_Item>();
        items.Add(new LstB_Item() { Title = "Items 1", Checked = false });
        items.Add(new LstB_Item() { Title = "Items 2", Checked = false });
        items.Add(new LstB_Item() { Title = "Items 3", Checked = false });
        LstB_Checklist.ItemsSource = items;
    }

    public class LstB_Item : INotifyPropertyChanged
    {
        public string Title { get; set; }

        private bool _checked;

        public bool Checked
        {
            get { return _checked; }
            set { _checked = value; NotifyPropertyChanged(); }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private void LstB_Checklist_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        txtb_Selection.Text = LstB_Checklist.SelectedItem.ToString();
    }

lisbox 已正确填充。我的问题是:获取选定项的值的正确代码是什么。

非常感谢您的支持

【问题讨论】:

  • 澄清一下,当前绑定返回一个列表框项目对象。所以我需要将它转换为简单的文本(选定的项目字符串)

标签: binding selecteditem listboxitem


【解决方案1】:

恐怕问题不太清楚或答案不明显,但我发布了一个答案,它可能会有所帮助:

可能的答案:

var selected = LstB_Checklist.SelectedItem as LstB_Item;
txtb_Selection.Text = selected.Title;

【讨论】:

    猜你喜欢
    • 2013-10-21
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 2011-08-23
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    相关资源
    最近更新 更多