XAML中的控件怎么绑定变量
XAML中的控件如何绑定变量
如:MainWindow.xaml中有一个TextBlock,MainWindow.xaml.cs中有一个变量public string MidleText { get; set; }
如何在xaml中把MidleText绑定到TextBlock的Text上,非C#代码中绑定。
------解决思路----------------------
用绑定加通知
xaml:
class:
cs:
------解决思路----------------------
不知道 看懂了没
如:MainWindow.xaml中有一个TextBlock,MainWindow.xaml.cs中有一个变量public string MidleText { get; set; }
如何在xaml中把MidleText绑定到TextBlock的Text上,非C#代码中绑定。
------解决思路----------------------
用绑定加通知
xaml:
<TextBlock Text="{Binding MidleText}" Width="100" Height="25"></TextBlock>
class:
public class Test : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string midleText;
public string MidleText
{
get { return midleText; }
set
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("MidleText"));
}
midleText = value;
}
}
}
cs:
public MainWindow()
{
InitializeComponent();
this.DataContext = new Test() { MidleText = "this is test" };
}
------解决思路----------------------
// 引用
xmlns:local="clr-namespace:wpf"
// 声明
public static string MidleText = "hello world";
// xaml
<TextBlock Width="120" Height="30" Text="{x:Static local:ButtonUserControl.MidleText}"></TextBlock>
不知道 看懂了没