将多个文本框文本绑定到单个列表或字符串数​​组

时间:2018-07-16 06:55:28

标签: c# wpf xaml binding

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

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

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

编辑#1:我要实现的是将不同文本框的文本绑定到单个实体。它可以是List,Array,Class或其他任何东西。

XAML:

$('#area').change(function(){
    var area = $('#area').val();
    $('select#demo option').each(function(){
    if(parseInt($(this).attr('data-area')) >= parseInt(area) ){
        $('select#demo').val($(this).val());
      return false;
    }
  })
})

C#:

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

1 个答案:

答案 0 :(得分: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中的一系列类绑定到您的View。

 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>

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