首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

|ZYCWPF| Interaction.Triggers 触发后盾属性更新后台属性时报错的总题,有示例

2012-11-07 
|ZYCWPF| Interaction.Triggers 触发后台属性更新后台属性时报错的总题,有示例XML codeWindow x:ClassS

|ZYCWPF| Interaction.Triggers 触发后台属性更新后台属性时报错的总题,有示例

XML code
<Window x:Class="SQRC_ViewModel觖发修改ViewModel属性.MainWindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"          xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"          Title="{Binding Title}" Height="350" Width="525">     <Grid>         <Border Name="brInkMessage" Grid.Column="1" Grid.Row="2">             <StackPanel>                 <TextBox x:Name="txt3" Text="{Binding IsLogin, UpdateSourceTrigger=PropertyChanged}" />                 <i:Interaction.Triggers>                     <ei:DataTrigger Binding="{Binding IsLogin}" Value="True">                         <ei:ChangePropertyAction TargetObject="{Binding}" PropertyName="Title"  Value="Changed" />                     </ei:DataTrigger>                 </i:Interaction.Triggers>             </StackPanel>         </Border>     </Grid> </Window>

C# code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;  namespace SQRC_ViewModel觖发修改ViewModel属性 {     /// <summary>     /// MainWindow.xaml 的交互逻辑     /// </summary>     public partial class MainWindow : Window     {         public MainWindow()         {             InitializeComponent();             this.DataContext = new MainWindowViewModel();         }     }      class MainWindowViewModel : NotificationObject     {         private string title;         /// <summary>         /// ??         /// </summary>         public string Title         {             get { return title; }             set             {                 title = value;                 this.RaisePropertyChanged("Title");             }         }          private bool isLogin;         /// <summary>         /// ??         /// </summary>         public bool IsLogin         {             get { return isLogin; }             set             {                 isLogin = value;                 this.RaisePropertyChanged("IsLogin");             }         }     }      class NotificationObject : INotifyPropertyChanged     {         public event PropertyChangedEventHandler PropertyChanged;          public void RaisePropertyChanged(string propertyName)         {             if (this.PropertyChanged != null)             {                 this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));             }         }     } } 


以上我的XAML中写了一个数据触发器,当IsLogin=true的时候将Title改为Changed
运行是正常的
但是把觖发器中的
<ei:DataTrigger Binding="{Binding IsLogin}" Value="True">
改为
<ei:DataTrigger Binding="{Binding IsLogin}" Value="False">
后就出现错误了
提示:Cannot find a property named "Title" on type "StackPanel"

这个要怎么解决

谢谢

PS:可能你们会说,我这样的业务逻辑不对,但是会存在这个情况,当多皮肤的时候,有一个皮肤想根据登录情况来自定自己的标题,这样是他自定义的其他模版都没有,所以在XAML中修改是比较好的情况

谢谢

------解决方案--------------------


你的stackpanel里面有 x:name=“Title”的元素么
没看到有啊?
[解决办法]
还是初始化时候取值的问题。
初始化时,DataContext为空,ChangePropertyAction找不到TargetObject,就在这个action所属的StackPanel上找Title,也找不到就报错。

解决办法还是把构造函数里的语句位置换下:
this.DataContext = new MainWindowViewModel();
InitializeComponent();

热点排行