All I am trying to do is bind a public property to a textBlock. What am I doing wrong here?
我要做的就是将公共属性绑定到textBlock。我在这做错了什么?
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public string test { get; set; }
public MainWindow()
{
test = "this is a test";
InitializeComponent();
}
}
}
<Window x:Class="WpfApplication1.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">
<Window.Resources>
<ObjectDataProvider x:Key="test"></ObjectDataProvider>
</Window.Resources>
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1" VerticalAlignment="Top" Text="{Binding Source={StaticResource test}}" />
</Grid>
3 个解决方案
#1
11
You can simply add a datacontext and access your property
您只需添加datacontext并访问您的属性即可
public partial class MainWindow : Window,INotifyPropertyChanged
{
private string _test;
public string test
{
get
{
return _test;
}
set
{
_test = value;
OnPropertyChanged("test");
}
}
public MainWindow()
{
test = "this is a test";
InitializeComponent();
DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
<TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1" VerticalAlignment="Top" Text="{Binding test}"/>
Also check this post for details of when to use an ObjectDataProvider
另请查看此帖子,了解何时使用ObjectDataProvider的详细信息
http://bea.stollnitz.com/blog/?p=22
http://bea.stollnitz.com/blog/?p=22
#2
6
At first you need you class to implement INotifyPropertyChanged or a property to be DependencyProperty for changing property value on textbox text change,
首先,您需要使用类来实现INotifyPropertyChanged或属性为DependencyProperty以更改文本框文本更改的属性值,
namespace WpfApplication1
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _test
public string test
{
get
{
return _test;
}
set
{
_test = value;
OnPropertyChanged("test");
}
}
public MainWindow()
{
test = "this is a test";
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
Than you can bind to that property by giving name to that window, and using ElementName property like this.
通过给该窗口命名,并使用像这样的ElementName属性,可以绑定到该属性。
<Window x:Class="WpfApplication1.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="myWindow">
<Window.Resources>
<ObjectDataProvider x:Key="test"></ObjectDataProvider>
</Window.Resources>
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1" VerticalAlignment="Top" Text="{Binding ElementName=myWindow, Path=test}" />
</Grid>
#3
0
You actually don't need to implement INotifyPropertyChanged. However, this will be a one time data binding.
实际上您不需要实现INotifyPropertyChanged。但是,这将是一次性数据绑定。
For example in XAML:
例如在XAML中:
<TextBlock Name="SomeTextBlock" Text="{Binding Path=SomeProp}" />
In Code:
在代码中:
public string SomeProp { get; set; }
public MainWindow()
{
InitializeComponent();
SomeProp = "Test Test Test";
SomeTextBlock.DataContext = this;
}
#1
11
You can simply add a datacontext and access your property
您只需添加datacontext并访问您的属性即可
public partial class MainWindow : Window,INotifyPropertyChanged
{
private string _test;
public string test
{
get
{
return _test;
}
set
{
_test = value;
OnPropertyChanged("test");
}
}
public MainWindow()
{
test = "this is a test";
InitializeComponent();
DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
<TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1" VerticalAlignment="Top" Text="{Binding test}"/>
Also check this post for details of when to use an ObjectDataProvider
另请查看此帖子,了解何时使用ObjectDataProvider的详细信息
http://bea.stollnitz.com/blog/?p=22
http://bea.stollnitz.com/blog/?p=22
#2
6
At first you need you class to implement INotifyPropertyChanged or a property to be DependencyProperty for changing property value on textbox text change,
首先,您需要使用类来实现INotifyPropertyChanged或属性为DependencyProperty以更改文本框文本更改的属性值,
namespace WpfApplication1
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _test
public string test
{
get
{
return _test;
}
set
{
_test = value;
OnPropertyChanged("test");
}
}
public MainWindow()
{
test = "this is a test";
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
Than you can bind to that property by giving name to that window, and using ElementName property like this.
通过给该窗口命名,并使用像这样的ElementName属性,可以绑定到该属性。
<Window x:Class="WpfApplication1.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="myWindow">
<Window.Resources>
<ObjectDataProvider x:Key="test"></ObjectDataProvider>
</Window.Resources>
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1" VerticalAlignment="Top" Text="{Binding ElementName=myWindow, Path=test}" />
</Grid>
#3
0
You actually don't need to implement INotifyPropertyChanged. However, this will be a one time data binding.
实际上您不需要实现INotifyPropertyChanged。但是,这将是一次性数据绑定。
For example in XAML:
例如在XAML中:
<TextBlock Name="SomeTextBlock" Text="{Binding Path=SomeProp}" />
In Code:
在代码中:
public string SomeProp { get; set; }
public MainWindow()
{
InitializeComponent();
SomeProp = "Test Test Test";
SomeTextBlock.DataContext = this;
}