在ListBox UWP xaml中控制datatemplate内部

问题描述 投票:0回答:1

我有一个由Datatemplate组成的ListBox。这是我的代码:

<ListBox x:Name="MyListBox" SelectionChanged="MyListBox_SelectionChanged_1"  SelectionMode="Single" IsItemClickEnabled="True">
<ListBox.ItemTemplate>
    <DataTemplate>
       <Rectangle Width="50" Height="45" Name="myRectangle"  Fill="DodgerBlue" />
       <Ellipse Grid.Column="0" Fill="LightGreen" Height="22" Width="22" />
       <TextBlock Text="{Binding Initials}" x:Name="PersonInitials" />
       <stackpanel>
       <TextBlock TextWrapping="Wrap" x:Name="person_lbl" Text="{Binding SourceLink}" Foreground="DodgerBlue"/>
       <TextBlock TextWrapping="Wrap" x:Name="detail_lbl" Text="{Binding ConversationId}" Foreground="DarkSlateGray"/>
       </StackPanel>
      <ToggleSwitch OffContent=" "/>
   </DataTemplate>           
</ListBox.ItemTemplate>
</ListBox> 

Datatemplate由矩形,椭圆,3个文本块和切换开关组成,最终给出如图所示的输出。 This is the template I will get.Please ignore the search bar.现在这是我的查询:

我想基于在listBox中选择data_template来执行操作,在该列表框中它应该获取文本框的文本并在某处显示它。在互联网上搜索之后我只发现了像

<listbox>item1</listbox>

我的ListBox不仅包含一个项目,它包含许多正在制作模板的项目。如何在选择整个模板时获取一个文本框元素。是否有人知道如何操作?这是我在c#中的代码:

    class ReadJsonData
    {
        public List<Information> information { get; set; }
        public ReadJsonData()
        {
            information = new List<Information>();

        }
    }
    public class Information
    {
        public string SourceLink { get; set; }
        public int ConversationId { get; set; }
        public string Initials { get; set; }
    }
 and this is on MainPage.xaml
    public MainPage()
        {
            this.InitializeComponent();
            JsonReader();

        }
        public async void JsonReader()
        {
            Information infos = new Information();
            infos.ConversationId = 1122;
            infos.Initials = "LM";
            infos.SourceLink = "abc";
            Information info1 = new Information();
            info1.ConversationId = 111;
            info1.Initials = "MM";
            info1.SourceLink = "pqr";
            Information info2 = new Information();
            info2.ConversationId = 1113;
            info2.Initials = "NM";
            info2.SourceLink = "vrl";
            ReadJsonData rb = new ReadJsonData();
            rb.information.Add(infos);
            rb.information.Add(info1);
            rb.information.Add(info2);
            var rb1 = JsonConvert.SerializeObject(rb);
            var rootobject = JsonConvert.DeserializeObject<ReadJsonData>(rb1);

            foreach (var info in rootobject.information)
            {
                MyListBox.ItemsSource = rootobject.information;

            }
        private void MyListBox_SelectionChanged(object sender,
        SelectionChangedEventArgs e)
        {
            var selectedItem = sender as Information;
            string initials = selectedItem.Initials;
            show_initial = initials;
        }
        }
c# xaml data-binding uwp
1个回答
0
投票

假设您将ListBox(MyListBox)的项目源设置为模型类(List<SomeClassName>)的项目列表。

如果你的模型类是这样的:

public class SomeClassName
    {
        public string Initials { get; set; }
        public string SourceLink { get; set; }
        public string ConversationId { get; set; }
    }

您可以将此代码添加到列表框的OnSelectionChangedTapped事件中,以获取该类的数据成员:

private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ 
   var selectedItem = sender as SomeClassName;
   //Gives you the Initials of the selected list item
   string intials = selectedItem.Initials;
}

希望这可以帮助..!


编辑1:

首先为什么要迭代rootobject.information?你可以简单地使用它作为项目源而不用for循环..

foreach (var info in rootobject.information)
{
   MyListBox.ItemsSource = rootobject.information;    
}

第二,这条线做什么? show_initial = initials;是show_initial变量?它有什么作用 ?


编辑2:

private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ 
    if(MyListBox.SelectedItem != null)
    {
      var selectedItem = sender as Information;
      //Gives you the Initials of the selected list item
      string intials = selectedItem.Initials;
    } else {
      Debug.WriteLine("No item Selected");
    }
}

请检查MyListBox.SelectedItem != null条件是否满足。

© www.soinside.com 2019 - 2024. All rights reserved.