WPF MVVM 模式下后台事件在前台的調用和綁定


方法一、事件觸發器(EventSetter)比較通用的方法不只限於MVVM模式

1、在前台樣式中定義事件觸發器並指定事件類型和事件名

 

 

 1是事件類型:這取決於樣式定義的是什么控件,不同的控件有不同的事件

2是要在后台編寫的事件名稱

2、前台定義好后在后台生成同名的方法,在其中編寫所有代碼就行了。

 

 

 方法二:利用依賴屬性的回調驗證方法

 

下面這段代碼采用用的是指定依賴屬性時,在初始化中指定回調函數,這樣做雖然方便但是必須是靜態方法,不便於在前台綁定

 public string InputValue
        {
            get { return (string)GetValue(InputValueProperty); }
            set { SetValue(InputValueProperty, value); }
        }

        public static readonly DependencyProperty InputValueProperty =
            DependencyProperty.Register("InputValue", typeof(string), typeof(A_Write_1), new UIPropertyMetadata(new PropertyChangedCallback(ValueChanged)));

        public static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            A_Write_1 my = d as A_Write_1;
            my.TransferVal(my.ValueConvert(e.NewValue.ToString()));
        }

 另外一種做法是

1、首先在后台定義一個依賴屬性

public bool AnimationTypeNO
{
get { return (bool)GetValue(AnimationTypeNOProperty); }
set { SetValue(AnimationTypeNOProperty, value); }
}

public static readonly DependencyProperty AnimationTypeNOProperty =
DependencyProperty.Register("AnimationTypeNO", typeof(bool), typeof(ParaSetView), new UIPropertyMetadata(false));

在構造函數中定義回調函數

DependencyPropertyDescriptor aaaaa = DependencyPropertyDescriptor.FromProperty(RadioButton.IsCheckedProperty, typeof(RadioButton));

aaaaa.AddValueChanged(aaaa, tbxEditMe_TextChanged);

 typeof(RadioButton):是回調函數要綁定的前台控件類型

RadioButton.IsCheckedProperty;是回調函數要綁定的前台控件的屬性(必須是依賴屬性)

aaaa:是前台要綁定的控件名

tbxEditMe_TextChanged:是前台要綁定的方法名

定義方法

private void tbxEditMe_TextChanged(object sender, EventArgs e)
{
...................
}

最后在前台添加對應控件並綁定依賴屬性就可以了

 

 

 方法三、Command綁定MVVM方式

1、在ViewMode中定義Command類和方法體

//定義繼承類ICommand接口的類屬性
 public RelayCommand<string> Init2Command { get; set; }
//定義方法體
 private void Init2(string ColumnsName)
        {
             //
        }
//在構造函數中生成時間委托
  public ParaSetViewModel()
        {
            Init2Command = new RelayCommand<string>(t => Init2(t));

        }

 2、在前台控件中定義事件觸發器就可以

// 綁定到ListBox的點擊事件
<ListBox x:Name="SubRoot" Background="{x:Null}" BorderBrush="{x:Null}" ItemsSource="{Binding ParaSet2PropertyList}" SelectedItem="{Binding ParaSet2Property}"
                        ItemContainerStyle="{StaticResource Catalogue1}">
//事件觸發器 <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged" > <i:InvokeCommandAction Command="{Binding Init2Command}" CommandParameter="{Binding ParaSet2Property.GROUP}" /> </i:EventTrigger> </i:Interaction.Triggers> <ListBox.ItemTemplate> <DataTemplate > <StackPanel Width="120"> <TextBlock Text="{Binding GROUP}" Tag="{Binding NodeText10}" Foreground="White" FontSize="18" Padding="5,0" HorizontalAlignment="Stretch" VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM