【问题标题】:Binding text blocks inside a user control using MVVM and WPF使用 MVVM 和 WPF 在用户控件内绑定文本块
【发布时间】:2017-12-08 03:29:51
【问题描述】:

我还是 MVVM 和 WPF 的新手 我查看了一些示例,但仍然没有找到我问题的确切答案。

我有一个ListBox,每个项目都应该使用ObservableCollection 添加一个新的用户控件。在用户控件中,我有几个文本块,我想将它们的文本绑定到包含数据的同一 ObservableCollection

但是我不确定如何将文本块绑定到 ObservableCollection

希望有代码示例。

我还附上了我的代码,这是我的 userControl XAML:

<UserControl x:Class="ProtocolAnalyzerGui.UserControlls.MenuControlls.UCSingleLine"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="300">
    <Grid Background="#FF454545">


    <Grid.RowDefinitions>
        <RowDefinition Height="1*"></RowDefinition>
        <RowDefinition Height="1*"></RowDefinition>
    </Grid.RowDefinitions>


    <TextBlock x:Name="TBHeader" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>

    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"></ColumnDefinition>
            <ColumnDefinition Width="10"></ColumnDefinition>
            <ColumnDefinition Width="1*"></ColumnDefinition>
            <ColumnDefinition Width="10"></ColumnDefinition>
            <ColumnDefinition Width="1*"></ColumnDefinition>
            <ColumnDefinition Width="10"></ColumnDefinition>
            <ColumnDefinition Width="1*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock x:Name="TBDatanTime" Grid.Column="0" Foreground="White" Text="{Binding DataAndTime }" ></TextBlock>
        <TextBlock x:Name="TBComPort" Grid.Column="2" Foreground="White"  Text="{Binding ComPort }" ></TextBlock>
        <TextBlock x:Name="TBTranslation" Grid.Column="4" Foreground="White" Text="{Binding Translation }" ></TextBlock>
        <TextBlock x:Name="TBDataBytesArray" Grid.Column="6" Foreground="White" Text="{Binding Header }" ></TextBlock>
    </Grid>
</Grid>
</UserControl>

在 XAML 主窗口中:

 <ScrollViewer Grid.Row="3" HorizontalScrollBarVisibility="Auto" 
VerticalScrollBarVisibility="Auto">
        <ListBox x:Name="LBListBox">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <myControlls:UCSingleLine x:Name="DataUserContoll" 
 DataContext="{Binding DataForGui}"></myControlls:UCSingleLine>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>
    </ScrollViewer>

列表框Itemsource的绑定在这个函数里面:

        private void MI_SerialPortStart_Click(object sender, RoutedEventArgs e)
        {
            LBListBox.ItemsSource = DataForGui;                
            _SerialPortTakeCare.Start();

        }

还附上我的数据代码:

public class Data : INotifyPropertyChanged
{   

        private string _DataAndTime;

        public string DataAndTime
        {
            get { return _DataAndTime; }
            set
            {
                _DataAndTime = value;
                OnPropertyChanged("DataAndTime");
            }
        }
        private string _ComPort;

        public string ComPort
        {
            get { return _ComPort; }
            set
            {
                _ComPort = value;
                OnPropertyChanged("ComPort");
            }
        }
        private string _Translation;

        public string Translation
        {
            get { return _Translation; }
            set
            {
                _Translation = value;
                OnPropertyChanged("Translation");
            }
        }
        private string _Header;

        public string Header
        {
            get { return _Header; }
            set
            {
                _Header = value;
                OnPropertyChanged("Header");
            }
        }
        private string _Data_ARR;

        public string Data_ARR
        {
            get { return _Data_ARR; }
            set
            {
                _Data_ARR = value;
                OnPropertyChanged("Data_ARR");

            }
        }

        public Data()
        {
            _ComPort = "";
            _Data_ARR = "";
            _DataAndTime = "";
            _Header = "";
            _Translation = "";
        }





    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
}

【问题讨论】:

  • 无法回答这个问题,因为代码不好minimal reproducible example。你甚至没有展示DataForGui 是什么。此外,当您尝试遵循他人给出的说明(在 cmets 或答案中)时,请不要编辑帖子中的代码。这样做改变了最初的问题,并且让后来出现的人很难理解你最初的问题,否定了在 Stack 上提出问题的价值,如果不是全部的话溢出。

标签: c# wpf xaml mvvm observablecollection


【解决方案1】:

您只需要设置用户控件的数据上下文,然后添加适当的绑定。

在您的主窗口中,此行应如下所示:

<local:UserControl1 DataContext="{Binding}" x:Name="DataUserContoll"/>

在用户控件中,代码应该更像这样:

<TextBlock x:Name="TBDatanTime" Grid.Column="0" Foreground="White" Text="{Binding TextField1}" />
<TextBlock x:Name="TBComPort" Grid.Column="2" Foreground="White" Text="{Binding TextField2}"/>
<TextBlock x:Name="TBTranslation" Grid.Column="4" Foreground="White" Text="{Binding TextField3}"/>
<TextBlock x:Name="TBDataBytesArray" Grid.Column="6" Foreground="White" Text="{Binding TextField4}"/>

...您应该将我输入的占位符字段名称(例如“TextField1”)更改为集合“DataForGui”中对象的实际字符串属性。

【讨论】:

  • 不...仍然不起作用,它添加了新控件,但没有添加里面的文本。
  • 编辑我的答案给你看。并添加了我的数据代码。可能有一些磨损的东西
  • 您更新的代码不包含 MainWindow XAML 中“
  • 抱歉忘记粘贴了。在编辑相同的问题后立即粘贴。也许它涉及命名空间?
  • 还是错了,你没有粘贴我给你看的。你把 DataContext="{Binding DataForGui}" 它应该是 DataContext="{Binding} 因为你将控件的数据上下文设置为来自 ItemsSource 的对象。
猜你喜欢
  • 1970-01-01
  • 2011-04-07
  • 2010-12-15
  • 1970-01-01
  • 1970-01-01
  • 2011-03-21
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多