如何从MainWindow中的用户Control获取数据

时间:2015-03-03 08:50:08

标签: c# wpf mvvm

我有两个控件的用户控件 标签 TextBlock

   <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绑定到此usercontrol的集合

 <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>

如果在列表框中选择了任何项目,我如何获得TextblockLabel的值?

2 个答案:

答案 0 :(得分:2)

这对我有用:

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

MainWindow.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>

MainWindow.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>

MyControl.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

答案 1 :(得分:0)

如果您想从所选项目中获取TextBlock,您可以这样做:

 var selectedUserControl = myListBox.SelectedItem as MyUserControl;
 TextBlock textBlock = selectedUserControl.MyTextBlock;
希望有所帮助!