我是 Xaml 和绑定概念的新手。如何在 XAML 中将 MainClass 的“CustomerName”属性与“TextBox1”的文本内容绑定?
这是我的 MainClass,
namespace TextBinding.Module
{
public class MainClass
{
public string CustomerName { get; set; }
}
}
我的 XAML 编码是,
<UserControl x:Class="TextBinding.Design.ControlDesigner"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:TestControl="clr-namespace:TextBinding.Module"
mc:Ignorable="d" d:DesignHeight="1000" d:DesignWidth="1000">
<TestControl:MainClass x:Key="Test1" />
<Grid>
<TextBox x:Name="TextBox1" Height="50" Text="{Binding Test1.CustomerName, Mode=TwoWay}" />
</Grid>
</UserControl>
上述方法根本不起作用。任何人都可以提出更好的绑定方法。?提前致谢。
<UserControl x:Class="TextBinding.Design.ControlDesigner"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:TestControl="clr-namespace:TextBinding.Module"
mc:Ignorable="d" d:DesignHeight="1000" d:DesignWidth="1000">
<UserControl.Resources>
<TestControl:MainClass x:Key="Test1" />
</UserControl.Resources>
<Grid>
<TextBox x:Name="TextBox1" Height="50" Text="{Binding CustomerName, Mode=TwoWay}"
DataContext="{DynamicResource Test1}" />
</Grid>
</UserControl>
您需要在 usercontrol 资源部分中定义类对象。您还需要在DataContext
文本框的属性中指定该类对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句