2016-09-05 45 views
0

我有自定義用戶控件,它必須在窗口中更改一些布爾變量並從某些綁定中獲取值。但是,當我第一次嘗試獲取值等於默認值 - null。當我採取行動時,它的效果很好。Catel WPF UserControl無法從依賴屬性中獲取值

<catel:UserControl x:Class="App.Shell.Controls.ToggleButton" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:catel="http://catel.codeplex.com" 
        xmlns:debug="clr-namespace:System.Diagnostics;assembly=System"> 
    <!-- Content --> 
    <Grid> 
     <Grid.Resources> 
      <Style TargetType="{x:Type Button}"> 
       <Setter Property="Width" Value="37" /> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type Button}"> 
          <Label 
           Margin="0" 
           Padding="0" 
           HorizontalContentAlignment="Center" 
           VerticalContentAlignment="Center" 
           Background="#010000" 
           Foreground="#FEFEFF" 
           FontSize="12"> 
           <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> 
          </Label> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding IsTurnedOn}" Value="True"> 
         <Setter Property="Content" Value="Turn off" /> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding IsTurnedOn}" Value="False"> 
         <Setter Property="Content" Value="Turn on" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </Grid.Resources> 

     <Button> 
      <Button.InputBindings> 
       <MouseBinding Command="{Binding ToggleCmd}" MouseAction="LeftDoubleClick" /> 
      </Button.InputBindings> 
     </Button> 
    </Grid> 
</catel:UserControl> 

與catel視圖模型

namespace App.Shell.Controls 
{ 
    using Catel.Data; 
    using Catel.MVVM; 
    using System; 
    using System.Windows.Input; 

    /// <summary> 
    /// UserControl view model. 
    /// </summary> 
    public class ToggleButtonViewModel : ViewModelBase 
    { 
     /// <summary> 
     /// Initializes a new instance of the <see cref="ToggleButtonViewModel"/> class. 
     /// </summary> 
     public ToggleButtonViewModel() 
     { 
      ToggleCmd = new Command(OnToggleCmdExecute); 
     } 

     /// <summary> 
     /// Gets the title of the view model. 
     /// </summary> 
     /// <value>The title.</value> 
     public override string Title { get { return "ToggleButton"; } } 

     public Command ToggleCmd { get; private set; } 
     private void OnToggleCmdExecute() 
     { 
      IsTurnedOn = !IsTurnedOn; 
     } 

     public bool IsTurnedOn 
     { 
      get { return GetValue<bool>(IsTurnedOnProperty); } 
      set { SetValue(IsTurnedOnProperty, value); } 
     } 
     public static readonly PropertyData IsTurnedOnProperty = RegisterProperty("IsTurnedOn", typeof(bool), setParent: false, createDefaultValue: null); 
     // TODO: Register models with the vmpropmodel codesnippet 
     // TODO: Register view model properties with the vmprop or vmpropviewmodeltomodel codesnippets 
     // TODO: Register commands with the vmcommand or vmcommandwithcanexecute codesnippets 
    } 
} 

和代碼背後

namespace App.Shell.Controls 
{ 
using Catel.Windows.Controls; 
using System.Windows; 
using System.Windows.Input; 
using Catel.MVVM.Views; 
using System; 
using Catel.MVVM; 

/// <summary> 
/// Interaction logic for ToggleButton.xaml. 
/// </summary> 
public partial class ToggleButton : UserControl 
{ 
    /// <summary> 
    /// Initializes a new instance of the <see cref="ToggleButton"/> class. 
    /// </summary> 
    public ToggleButton() 
    { 
     InitializeComponent(); 
    } 

    [ViewToViewModel(MappingType = ViewToViewModelMappingType.TwoWayViewWins)] 
    public bool IsTurnedOn 
    { 
     get { return (bool)GetValue(IsTurnedOnProperty); } 
     set { SetValue(IsTurnedOnProperty, value); } 
    } 
    public static readonly DependencyProperty IsTurnedOnProperty = 
     DependencyProperty.Register("IsTurnedOn", typeof(bool), typeof(ToggleButton), new PropertyMetadata(null)); 
} 

}

我插入控制我的catel窗口

<ctrls:ToggleButton IsTurnedOn="{Binding Path=IndividualSpeechTimerState, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" /> 

當我嘗試從第一次從UserControl屬性IsTurnedOn它等於默認值。

+0

Null對於bool不是有效的值。也許這是導致DependencyProperty的一些問題? – ibebbs

+0

不,我很努力。綁定不設置值爲dp只是使用默認值always –

回答

0
[ViewToViewModel(MappingType = ViewToViewModelMappingType.ViewModelToView)] 
    public bool IsTurnedOn 
    { 
     get { return (bool)GetValue(IsTurnedOnProperty); } 
     set { SetValue(IsTurnedOnProperty, value); } 
    } 
    public static readonly DependencyProperty IsTurnedOnProperty = 
     DependencyProperty.Register("IsTurnedOn", typeof(bool), typeof(ToggleButton), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsTurnedOnChanged)); 

    private static void OnIsTurnedOnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var btn = d as ToggleButton; 
     var vm = btn.ViewModel as ToggleButtonViewModel; 
     if(vm.IsTurnedOn != btn.IsTurnedOn) 
      vm.IsTurnedOn = btn.IsTurnedOn; 
    } 
0

由於您定義了[ViewToViewModel(MappingType = ViewToViewModelMappingType.TwoWayViewWins)],它總是會寫入視圖的第一次屬性。這允許您在外部綁定此值。

請確保在您的依賴項屬性定義中設置有效值(例如true或false)。

+0

是的,我需要外部綁定。但值等於vm的默認值 –