绑定到组合框控件模板项失败

本文关键字:失败 控件 组合 绑定 | 更新日期: 2023-09-27 17:56:11

我正在使用组合框并通过列表值绑定,如下所示

    <ComboBox ItemsSource="{Binding}" Name="testCombo" Margin="67,48,184,204">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBox  Text="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=ComboBox},Mode=OneWay,Path=SelectedItem}" Width="100" Height="50" />
            </DataTemplate>
        </ComboBox.ItemTemplate>            
    </ComboBox>

在 C# 中绑定如下所示的数据

      IList<string> objLoadData = new List<String> { "Ramesh", "Suresh" };
      testCombo.DataContext = objLoadData;    

我试图将数据绑定到文本框中,但我不能。如何将数据绑定到文本框?

绑定到组合框控件模板项失败

试试这个;

内部结构器

testCombo.DataContext = this;
testCombo.ItemsSource = objLoadData;  

在 XAML 中

   <ComboBox Name="testCombo" Width="100" Height="50" >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Mode=OneWay}" Width="100" Height="50" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

如果你的列表只是字符串,Text="{Binding}"应该可以工作

您应该考虑为您的ItemsSource使用ObservableCollection,因为它会在项目更改(添加/删除等)时自动更新ComboBox

<Window x:Class="WpfApplication13.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <Grid DataContext="{Binding ElementName=UI}">
        <ComboBox ItemsSource="{Binding ComboItems}" Margin="67,48,184,204">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" Width="100" Height="50" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>

法典:

public partial class MainWindow : Window
{
    private ObservableCollection<string> _comboItems = new ObservableCollection<string>();
    public MainWindow()
    {
        InitializeComponent();
        ComboItems.Add("Stack");
        ComboItems.Add("Overflow");
    }
    public ObservableCollection<string> ComboItems
    {
        get { return _comboItems; }
        set { _comboItems = value; }
    }
}

如果要在ComboBox中使用TextBox,则可能需要使用对象作为字符串的支持存储,该字符串以这种方式实现INotifyPropertyChanged更新并保持同步。

例:

<Window x:Class="WpfApplication13.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <Grid DataContext="{Binding ElementName=UI}">
        <ComboBox ItemsSource="{Binding ComboItems}" Margin="67,48,184,204">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Name}" Width="100" Height="50" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>

法典:

public partial class MainWindow : Window
{
    private ObservableCollection<MyItem> _comboItems = new ObservableCollection<MyItem>();
    public MainWindow()
    {
        InitializeComponent();
        ComboItems.Add(new MyItem { Name = "Stack" });
        ComboItems.Add(new MyItem { Name = "Overflow" });
    }
    public ObservableCollection<MyItem> ComboItems
    {
        get { return _comboItems; }
        set { _comboItems = value; }
    }
}
public class MyItem : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; NotifyPropertyChanged("Name"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}