[英]Binding to the combobox control template item failed
我正在使用組合框並通過List值綁定,如下所示
<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;
我試圖將數據綁定到文本框中,但我不能。 如何將數據綁定到文本框?
如果你的列表只是字符串,那么Text="{Binding}"
可以了
您應該考慮為ItemsSource
使用ObservableCollection
,因為它會在Items更改時自動更新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
的字符串的后備存儲,這樣eveyything就會被更新並保持同步。
例:
<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));
}
}
}
嘗試這個;
內部構造
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.