2013-07-12 68 views
0

我是wpf的新手,我尝试使用DataTemplate创建菜单,以了解如何使用DataTemplates。BindingExpression路径错误:'对象'上找不到'DisplayName'属性

我得到以下绑定错误,并没有文字出现在控制中,我看不出为什么。

绑定错误

System.Windows.Data Error: 40 : BindingExpression path error: 'DisplayName' property not found on 'object' ''StartOptionsViewModel' (HashCode=7730701)'. BindingExpression:Path=DisplayName; DataItem='StartOptionsViewModel' (HashCode=7730701); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 

MainWindow.XAML是:

<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Colin's Test Application" Width="600" Height="400"> 
    <Window.Resources> 

    <!-- WIZARD STEP TEMPLATE --> 
     <DataTemplate x:Key="OptionsTemplate"> 
      <Border x:Name="bdOuter" BorderBrush="Black" BorderThickness="0,0,1,1" CornerRadius="12" Margin="1,1,1,12" Opacity="1" SnapsToDevicePixels="True" > 
       <Border x:Name="bdInner" Background="#FFFEFEFE" BorderBrush="Brown" BorderThickness="2,2,1,1" CornerRadius="12" Padding="2" > 
        <TextBlock x:Name="txt" Margin="4,0,0,0" Foreground="Black" Text="{Binding Path=DisplayName, Mode=OneWayToSource}" /> 
       </Border> 
      </Border> 
     </DataTemplate> 
    </Window.Resources> 

    <StackPanel VerticalAlignment="Center"> 
     <ItemsControl 
     ItemsSource="{Binding Path=StartOptions}" 
     ItemTemplate="{StaticResource OptionsTemplate}" 
     /> 
    </StackPanel> 
</Window> 

我MainWindoViewModel.cs

public partial class MainWindowViewModel 
{ 
    private List<StartOptionsViewModel> _listStartOptionsVM = new List<StartOptionsViewModel>(); 

    public MainWindowViewModel() 
    { 
     _listStartOptionsVM.AddRange(new StartOptionsViewModel[] 
     { 
      new StartOptionsViewModel(new StartOption("New Application", StartOption.StartOptionTypes.Button)), 
      new StartOptionsViewModel(new StartOption("Exit Application", StartOption.StartOptionTypes.Button)) 
     }); 
    } 

    public ReadOnlyCollection<StartOptionsViewModel> StartOptions 
    { 
     get 
     { 
      return new ReadOnlyCollection<StartOptionsViewModel>(_listStartOptionsVM); 
     } 
    } 
} 

` StartOptionsViewModel.cs:

public class StartOptionsViewModel 
{ 
    private StartOption m_startOption = null; 
    public StartOptionsViewModel(StartOption p_startOption) 
    { 
     m_startOption = p_startOption; 
    } 

    #region Properties 

    public string DisplayName 
    { 
     get { return m_startOption.DisplayName; } 
     set 
     { 
      //... 
     } 

    } 

    #endregion 
} 
+0

'OneWayToSource'从'Xaml'绑定到'object','OneWay'从'object'到'Xaml',在你的情况下你想使用'OneWay',因为你没有设置对象和“TextBlock”无法编辑。 –

回答

0

首先,你是以错误的方式绑定你的文本块模式,因为文本框文本永远不会从ui的意思在运行时由用户儿子它'smode不能onewaytosource它应该是一种方式,所以首先你应该有正确的,像这..

<TextBlock 
    x:Name="txt" 
    Margin="4,0,0,0" 
    Foreground="Black" 
    Text="{Binding Path=DisplayName, Mode=OneWay}" 
    /> 

首先使它正确并运行你的代码和评论,如果你的问题依然存在。

+0

感谢您的回复。我已经改变了你的建议,但我仍然遇到同样的问题。 – user1683456

+0

我不明白这是因为从错误它指向正确的类,并看着正确的属性,我知道属性存在于类和返回类型是正确的。 – user1683456

+0

它的工作。不知道我做了什么。 – user1683456

相关问题