0
我無法弄清楚爲什麼我不斷收到此錯誤,並且當我在GUI中對tb_Name
TextBox
進行更改時,永遠不會調用Name
中的設置。WPF文本框綁定錯誤
System.Windows.Data Error: 40 : BindingExpression path error: 'Name' property not found on 'object' ''String' (HashCode=2106982518)'. BindingExpression:Path=Name; DataItem='String' (HashCode=2106982518); target element is 'TextBox' (Name='tb_Name'); target property is 'Text' (type 'String')
XAML
<UserControl
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:local="clr-namespace:ConfigStudioUI.Controls" x:Class="ConfigStudioUI.Controls.DeviceTypeTabCtrl"
mc:Ignorable="d" x:Name="DeviceTypeUC" Loaded="DeviceType_Loaded"
d:DesignHeight="400" d:DesignWidth="600" Background="#FF00C8FF"
>
<Grid>
<TabControl Background="#FF00FF99" FontSize="14"
TabStripPlacement="Left" Margin="0, 0, 0, 10" >
<TabItem Name="PropertiesTab" Header="Properties">
<Grid>
<Grid >
<TextBox Text="{Binding Source=DeviceType, Path=Name, Mode=TwoWay}"
TabIndex="0" x:Name="tb_Name" HorizontalAlignment="Stretch" Height="32"
Margin="159,28,5.2,0" VerticalAlignment="Top" />
</Grid>
</Grid>
</TabItem>
</TabControl>
</UserControl>
代碼隱藏
public partial class DeviceTypeTabCtrl : UserControl
{
public DeviceType DeviceType { get; set; }
public DeviceTypeTabCtrl(DeviceType deviceTypeObject, DeviceTypeGroup
deviceTypeGroupObject)
{
InitializeComponent();
DataContext = this;
this.DeviceType = new DeviceType();
this.DeviceType = deviceTypeObject;
this.tb_Name.Text = deviceTypeObject.Name;
this.DeviceType.DeviceTypeGroupGUID =
deviceTypeGroupObject.DeviceTypeGroupGUID;
}
}
public class DeviceType : INotifyPropertyChanged
{
/// <summary>
/// Name
/// </summary>
public string Name
{
get
{
return this.name;
}
set
{
if (this.name != value)
{
this.name = value;
NotifyPropertyChanged("Name");
}
}
}
}
該錯誤似乎表明您的綁定的Source的DeviceType是一個String而不是您的DeviceType類的實例(這就是爲什麼它說Name不存在)。你有沒有特別的理由在你的綁定中使用'Source',而不是讓'DataContext'自然流動?是否有可能在XAML中的某個地方使用「ResourceType」的「key」定義了「Resource」?如果是這樣,那將是挑選。 –
你能向我們展示更多來自你的'UserControl'的XAML嗎?我想看看'TextBox'的所有祖先是如何綁定的,以及'DataContext'來自何處。 –
NotifyPropertyChanged,不應該是OnPropertyChanged? – stuicidle