好吧,我有一个WPF项目,其中我有4个TexBlock.我想要的是改变Text每个TextBlock通道Binding.
到目前为止,我有我的XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="First" Text="{Binding FirstString}" Grid.Row="0"/>
<TextBlock x:Name="Second" Text="{Binding SecondString}" Grid.Row="1"/>
<TextBlock x:Name="Third" Text="{Binding ThirdString}" Grid.Row="2"/>
<TextBlock x:Name="Fourth" Text="{Binding FourthString}" Grid.Row="3"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
在我的代码中我有:
public partial class MainWindow : Window
{
public string FirstString { get; set; }
public string SecondString { get; set; }
public string ThirdString { get; set; }
public string FourthString { get; set; }
public MainWindow()
{
InitializeComponent();
FirstString = "First";
SecondString = "Second";
ThirdString= "Third
FourthString= "Fourth";
}
}
Run Code Online (Sandbox Code Playgroud)
但Binding根本不起作用.我做错了吗?请帮忙.提前致谢.
EDIT:
Run Code Online (Sandbox Code Playgroud)
按照Chris Mantle的建议查看debbuger(我已将其设置为Warning sor the Binding)后,我收到以下错误:
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=FirstString; DataItem=null; target element is 'TextBlock' (Name='First'); target property is 'Text' (type 'String')
Run Code Online (Sandbox Code Playgroud)
Sim*_*ger 13
有一些事情是不正确的.该Binding标记将着眼于对象的DataContext控制的财产.DataContext除非另有说明,否则此属性将继承声明父级.开箱即用,这是null一个Window控件.
这个问题有两种选择.您可以DataContext在代码隐藏或XAML中明确设置
// In XAML
<Window DataContext={Binding RelativeSource={RelativeSource Self}}>
or
// In the code-behind
DataContext = this;
Run Code Online (Sandbox Code Playgroud)
另一个问题是在初始化时应用绑定.最初,您的属性为空.在InitializeComponent阶段之后,控件将"绑定"到属性(它们是空的).之后设置属性时,控件无法知道它已更改.有两种机制可以实现这一点.在控件级别,您可以将这些属性设置为DependencyProperty或实现INotifyPropertyChanged接口并引发更改.如果你想进入INPC路线,你可以实现你的属性和Window:
public partial class MainWindow : INotifyPropertyChanged
{
private string firstString;
private string secondString;
private string thirdString;
private string fourthString;
public string FirstString
{
get { return firstString; }
set
{
firstString = value;
RaisePropertyChanged("FirstString");
}
}
public string SecondString
{
get { return secondString; }
set
{
secondString = value;
RaisePropertyChanged("SecondString");
}
}
public string ThirdString
{
get { return thirdString; }
set
{
thirdString = value;
RaisePropertyChanged("ThirdString");
}
}
public string FourthString
{
get { return fourthString; }
set
{
fourthString = value;
RaisePropertyChanged("FourthString");
}
}
public MainWindow()
{
DataContext = this;
InitializeComponent();
FirstString = "First";
SecondString = "Second";
ThirdString = "Third";
FourthString = "Fourth";
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void RaisePropertyChanged(string propertyName)
{
var handlers = PropertyChanged;
handlers(this, new PropertyChangedEventArgs(propertyName));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23598 次 |
| 最近记录: |