【问题标题】:Binding multiple Textbox texts to a single List or String Array将多个文本框文本绑定到单个列表或字符串数​​组
【发布时间】:2018-07-16 06:55:27
【问题描述】:

我的所有 DataGrid 标题中都有一个 TextBox。

我能够使用以下代码将每个 TextBox 文本绑定到不同的字符串。

但我想将所有文本绑定(两种方式)到单个列表或字符串数​​组。有什么办法吗?

EDIT #1 : 我想要实现的是将不同文本框的文本绑定到单个实体。它可以是列表、数组、类或任何东西。

XAML:

<DataGrid Name="dataGrid" >
    <DataGrid.Columns>
        <DataGridTextColumn  Header="Name" Binding="{Binding Name}">
            <DataGridTextColumn.HeaderTemplate >
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>                                    
                        <TextBox  Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:TestDataGrid}}, Path=strName}"/>                                        
                    </StackPanel>
                </DataTemplate>
            </DataGridTextColumn.HeaderTemplate>
        </DataGridTextColumn>

        <DataGridTextColumn  Header="Age" Binding="{Binding Age}">
            <DataGridTextColumn.HeaderTemplate>
                <DataTemplate>
                    <StackPanel >
                        <<TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>                                    
                        <TextBox  Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:TestDataGrid}}, Path=strAge}"/>           
                    </StackPanel>
                </DataTemplate>
            </DataGridTextColumn.HeaderTemplate>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

C#:

public partial class TestDataGrid : Page
{
    public String strName { get; set; }
    public String strAge { get; set; }

    public TestDataGrid()
    {
        InitializeComponent();                                  
    }

    onSomeEvent ()
    {
        MessageBox.Show("" + strName);
        MessageBox.Show("" + strAge);
    }
}

【问题讨论】:

  • 实现这一目标的唯一方法是使用具有多重绑定的转换器,以便传递数组和列表/整个列表中的索引。实现两种方式绑定到列表/字符串数组是一件痛苦的事情。为什么不使用包含您的字符串值的类,并且应该通知?
  • 要获得双向绑定,您必须实现 INotifyPropertyChanged,所以我认为这不符合您的要求。
  • @Sajith Sageer 不清楚要绑定什么?文本框的两种方式的字符串列表?
  • @Rekshino 我想将所有 texbox 中的文本绑定到一个列表中,它可以是字符串列表
  • @LupuSilviu 我也可以使用类。我已经编辑了这个问题。我也不知道如何将它们绑定到一个类

标签: c# wpf xaml binding


【解决方案1】:

每个条目都需要一个类,

   class MyEntry : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private String _strName, _strAge;

    public String StrAge
    {
        get { return _strAge; }
        set
        {
            _strAge = value;
            RaisePropertyChanged("StrAge");
        }
    }

    public String StrName
    {
        get { return _strName; }
        set
        {
            _strName = value;
            RaisePropertyChanged("StrName");
        }
    }

    public MyEntry()
    {

    }

    public MyEntry(String name, String age)
    {
        StrName = name;
        StrAge = age;
    }

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

然后,您需要在将绑定到您的视图的 ViewModel 中的类数组。

 class ViewModel
{
    public List<MyEntry> ListOfEntries { get; set; }

    public ViewModel()
    {
        ListOfEntries = new List<MyEntry>();
        AddDefaultEntries();
    }

    private void AddDefaultEntries()
    {
        ListOfEntries.Add(new MyEntry("Candidate1", "29"));
        ListOfEntries.Add(new MyEntry("Candidate2", "20"));
    }
}

然后将 ViewModel 绑定到 View 列表中:

    public partial class MainWindow : Window
{
    private ViewModel _viewModel;
    public MainWindow()
    {

        InitializeComponent();
        _viewModel = new ViewModel();
        this.DataContext = _viewModel;
    }
}

XAML 代码更改如下:

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

    <DataGrid Grid.Row="0"
              ItemsSource="{Binding ListOfEntries}"
              AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn  Header="Name" Binding="{Binding StrName}"/>
            <DataGridTextColumn  Header="Age" Binding="{Binding StrAge}"/>
        </DataGrid.Columns>
    </DataGrid>

    <ListView Grid.Row="1" ItemsSource="{Binding ListOfEntries}"
              IsHitTestVisible="False" Focusable="False">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding StrName}"/>
                    <GridViewColumn Header="Age" DisplayMemberBinding="{Binding StrAge}"/>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

</Grid>

希望这至少有助于引导您走向正确的方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2012-04-19
    • 2014-03-19
    • 2013-02-11
    相关资源
    最近更新 更多