gpt4 book ai didi

c# - 如何从 MainWindow 中的用户控件获取数据

转载 作者:行者123 更新时间:2023-12-03 10:27:56 27 4
gpt4 key购买 nike

我有用户控件,它有两个控件
标签 文本 block

   <UserControl x:Class=MyClass....
d:DesignHeight="300" d:DesignWidth="300" x:Name="MyUsrCtrl">
<StackPanel>
<Label Content={Binding MyLabelContent} x:Name="MyLabel"...../>
<TextBlock Content={Binding MyTextBlockContent} x:Name="MyTextBlock"...../>
</StackPanel>

</UserControl>

并且在我的 MainWindow 中,我有一个 ListBox,其 ItemSource 绑定(bind)到此用户控件的集合
 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

<Grid>
<ListBox x:Name="myListBox" Grid.Row="0"
ItemsSource="{Binding Path=_myControl}"> // collection of user controls
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<local:MyUserControl x:Name="myUserControl" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>

</ListBox>

如何获得 Textblock 的值和 Label在列表框中选择任何项目时?

最佳答案

这对我有用:

  • 创建了一个名为 MyControl 的模型,它“代表”了 MyUserControl
  • 中的数据
  • 创建了一个“表示”列表框中数据的 ObservableCollection
  • 这样你也可以删除所有的 x:Name
  • 从 UI 中分离数据

  • 主窗口.xaml
       <ListBox x:Name="MyListBox" Grid.Row="0"
    ItemsSource="{Binding MyControls}"
    SelectionChanged="MyListBox_OnSelectionChanged"
    SelectionMode="Single"
    >
    <ListBox.ItemTemplate>
    <DataTemplate>
    <Grid>
    <local:MyUserControl></local:MyUserControl>
    </Grid>
    </DataTemplate>
    </ListBox.ItemTemplate>

    </ListBox>

    主窗口.cs
       public partial class MainWindow : Window, INotifyPropertyChanged
    {
    public MainWindow()
    {
    InitializeComponent();
    this.DataContext = this;

    MyControls = new ObservableCollection<MyControl>();
    var a = new MyControl { MyLabelContent = "label content 1", MyTextBlockContent = "Text content 1" };
    var b = new MyControl { MyLabelContent = "label content 2", MyTextBlockContent = "Text content 2" };

    MyControls.Add(a);
    MyControls.Add(b);

    }


    private void MyListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    var listBox = sender as ListBox;
    if (listBox != null)
    {
    var selectedItem = listBox.SelectedItems[0] as MyControl;

    var textBlockContent = selectedItem.MyTextBlockContent; //text in textblock
    var labelContent = selectedItem.MyLabelContent; //text in label

    }
    }


    private ObservableCollection<MyControl> _myControls;


    public ObservableCollection<MyControl> MyControls
    {
    get { return _myControls; }
    set
    {
    _myControls = value;
    NotifyPropertyChanged("MyControls");
    }
    }


    #region PropertyChanged implementation

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
    if (PropertyChanged != null)
    {
    PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
    }


    #endregion


    }

    MyUserControl.XAML
       <StackPanel>
    <Label Content="{Binding MyLabelContent}" />
    <TextBlock Text="{Binding MyTextBlockContent}" />
    </StackPanel>

    我的控制.cs
     public class MyControl
    {
    public string MyLabelContent { get; set; }
    public string MyTextBlockContent { get; set; }

    }

    希望这对你有用:)

    这是一个工作示例的链接:
    https://drive.google.com/file/d/0B8O-XH0V_o1hNXprX2c0S0xJUFU/view?usp=sharing

    关于c# - 如何从 MainWindow 中的用户控件获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28827503/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com