绑定到 ComboBox 中的 TextBlock 的字符串未显示
string bound to TextBlock in ComboBox doesn't show up
我在 C# WPF 项目的 ComboBox 中有一个 TextBlock 绑定到 'Envelope' 项列表,其中有一个字符串 'Name' 和一个双 'Weight' 属性, 我希望在 TextBlock 中看到前者。
当我 运行 我的程序时,组合框出现时没有任何文本。它正确地包含三个未标记的项目,如果我查看 ComboBox 的 ItemsSource 或 SelectedItem,它们会显示适当的值,并且与 ComboBox 的 SelectedItem 交互的其他代码行为正常。唯一不起作用的是 TextBlock 不包含文本。如果我将 "{Binding Name}"
替换为 "au ghdfjlnvgmumar"
,则 ComboBox 中会出现相应的乱码,因此这肯定是绑定问题。问题是什么,我该如何解决?
相关代码:
xaml:
<ComboBox Name="EnvelopeList" HorizontalAlignment="Center" Width="200" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
C#:
//main window code
public MainWindow()
{
InitializeComponent();
envelopes = new List<Envelope>();
envelopes.Add(new Envelope("TEST", 0));
envelopes.Add(new Envelope("HI", 10));
EnvelopeList.ItemsSource = envelopes;
}
//Envelope class
class Envelope
{
public string Name;
public double Weight;
public Envelope()
{
Name = "[None]";
Weight = 0;
}
public Envelope(string n, double w)
{
Name = n;
Weight = w;
}
public override string ToString()
{
return Name;
}
}
Name
是一个字段,你只能绑定到properties.
当DataBinding
时,您只能绑定到属性。此外,您需要使用 PropertyChangedEvent
更新您的属性。否则,如果您在初始绑定后更改 属性,它不会更新 UI。
你需要使用 on 属性 changed and a 属性
public class Envelope: ModelBase
{
private string _name;
public string Name
{
get { return _name; }
set { _name= value; OnPropertyChanged("Name"); }
}
}
public class ModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propName));
}
}
}
最后,我注意到您直接设置了 ItemsSource
。相反,您想设置视图的 DataContext
属性 然后绑定到您的 ItemsSource
这里有一个 MSDN article on DataBinding 会教你如何正确地做到这一点。
我在 C# WPF 项目的 ComboBox 中有一个 TextBlock 绑定到 'Envelope' 项列表,其中有一个字符串 'Name' 和一个双 'Weight' 属性, 我希望在 TextBlock 中看到前者。
当我 运行 我的程序时,组合框出现时没有任何文本。它正确地包含三个未标记的项目,如果我查看 ComboBox 的 ItemsSource 或 SelectedItem,它们会显示适当的值,并且与 ComboBox 的 SelectedItem 交互的其他代码行为正常。唯一不起作用的是 TextBlock 不包含文本。如果我将 "{Binding Name}"
替换为 "au ghdfjlnvgmumar"
,则 ComboBox 中会出现相应的乱码,因此这肯定是绑定问题。问题是什么,我该如何解决?
相关代码:
xaml:
<ComboBox Name="EnvelopeList" HorizontalAlignment="Center" Width="200" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
C#:
//main window code
public MainWindow()
{
InitializeComponent();
envelopes = new List<Envelope>();
envelopes.Add(new Envelope("TEST", 0));
envelopes.Add(new Envelope("HI", 10));
EnvelopeList.ItemsSource = envelopes;
}
//Envelope class
class Envelope
{
public string Name;
public double Weight;
public Envelope()
{
Name = "[None]";
Weight = 0;
}
public Envelope(string n, double w)
{
Name = n;
Weight = w;
}
public override string ToString()
{
return Name;
}
}
Name
是一个字段,你只能绑定到properties.
当DataBinding
时,您只能绑定到属性。此外,您需要使用 PropertyChangedEvent
更新您的属性。否则,如果您在初始绑定后更改 属性,它不会更新 UI。
你需要使用 on 属性 changed and a 属性
public class Envelope: ModelBase
{
private string _name;
public string Name
{
get { return _name; }
set { _name= value; OnPropertyChanged("Name"); }
}
}
public class ModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propName));
}
}
}
最后,我注意到您直接设置了 ItemsSource
。相反,您想设置视图的 DataContext
属性 然后绑定到您的 ItemsSource
这里有一个 MSDN article on DataBinding 会教你如何正确地做到这一点。