2017-03-29 68 views
0

我已經搜查了這個問題很多。但是,似乎沒有解決方案爲我工作。我沒有得到任何錯誤,但我添加了一個斷點到我的IValueConverter。斷點不會被觸發。爲什麼不使用我的轉換器?我想要做的就是使用視圖模型策略來查看UI元素的綁定(在這種情況下是複選框)。任何幫助表示讚賞。綁定布爾能見度財產

的IValueConverter:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Data; 

namespace Test_Tool.Common 
{ 
    public class BooleanToVisibilityConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, string language) => (bool)value^(parameter as string ?? string.Empty).Equals("Reverse") ? Visibility.Visible : Visibility.Collapsed; 

     public object ConvertBack(object value, Type targetType, object parameter, string language) => (Visibility)value == Visibility.Visible^(parameter as string ?? string.Empty).Equals("Reverse"); 
    } 
} 

XAML:

<Page 
    x:Class="Test_Tool.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:Test_Tool" 
    xmlns:converter="using:Test_Tool.Common" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <Grid.Resources> 
      <converter:BooleanToVisibilityConverter x:Key="cvt" /> 
     </Grid.Resources> 
     <Pivot x:Name="rootPivot" Title="Test Tool" > 
      <PivotItem Header="Test Selection"> 
       <StackPanel> 
        <CheckBox x:Name="dppCheckBox" Content="DPP" Margin="5,8,5,5" Visibility="{Binding IsDirect, Converter={StaticResource cvt}}" /> 
       </StackPanel> 
      </PivotItem> 
     </Pivot> 
    </Grid> 
</Page> 

視圖模型:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Runtime.CompilerServices; 
using System.Text; 
using System.Threading.Tasks; 

namespace Test_Tool.ViewModels 
{ 
    public class MainPageViewModel : INotifyPropertyChanged 
    { 
     //Localized private vars 
     private bool _isDirect; 

     //Public vars for bindings 
     public bool IsDirect 
     { 
      get 
      { 
       return _isDirect; 
      } 
      set 
      { 
       _isDirect = value; 
       OnPropertyChanged(); 
      } 
     } 

     public MainPageViewModel() 
     { 
      //Any Initialization 
      IsDirect = false; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     void OnPropertyChanged([CallerMemberName]string propertyName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

的MainPage:

using DM_API_Test_Tool.ViewModels; 
using Windows.UI.Xaml.Controls; 

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 

namespace Test_Tool 
{ 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class MainPage : Page 
    { 
     public MainPageViewModel ViewModel { get; set; } 
     public MainPage() 
     { 
      this.InitializeComponent(); 
      this.ViewModel = new MainPageViewModel(); 
     } 

    } 
} 
+0

您是否嘗試過做一個CLR命名空間聲明,而不是一個簡單的使用? 相反的: 的xmlns:轉換器= 「使用:Test_Tool.Common」 用途: 的xmlns:轉換器= 「CLR的命名空間:Test_Tool.Common」 – exceptionthrown

+0

@exceptionthrown可惜的是沒有解決它。該複選框仍然可見,並且我的轉換器中的斷點從未被擊中 – Kyle

+0

您可能需要指定哪些屬性已更改。而不是:OnPropertyChanged()使用:OnPropertyChanged(nameof(IsDirect))。 – exceptionthrown

回答

1

試試這個

namespace Test_Tool.ViewModels 
{ 
    public class MainPageViewModel : INotifyPropertyChanged 
    { 
     private bool _isDirect = false; 
     public bool IsDirect 
     { 
      get 
      { 
       return _isDirect; 
      } 
      set 
      { 
       set { SetField(ref _isDirect, value, "isDirect"); } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
     protected bool SetField<T>(ref T field, T value, string propertyName) 
     { 
      if (EqualityComparer<T>.Default.Equals(field, value)) return false; 
      field = value; 
      OnPropertyChanged(propertyName); 
      return true; 
     } 
    } 
} 

這應該現在的工作。

不過我更喜歡BindableBase方法更(保存在一個新的類下面的內容並把它像BindableBase.cs

using System; 
using System.ComponentModel; 
using System.Runtime.CompilerServices; 

namespace Mvvm 
{ 
    public abstract class BindableBase : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null) 
     { 
      if (object.Equals(storage, value)) return false; 

      storage = value; 
      this.OnPropertyChanged(propertyName); 
      return true; 
     } 

     protected void OnPropertyChanged([CallerMemberName] string propertyName = null) 
     { 
      var eventHandler = this.PropertyChanged; 
      if (eventHandler != null) 
      { 
       eventHandler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
} 

,然後你ViewModel將減少到

using Mvvm; 

namespace Test_Tool 
{ 
    public class MainPageViewModel : BindableBase 
    { 
     private bool _isDirect = false; 
     public bool IsDirect 
     { 
      get { return _isDirect; } 
      set { SetProperty(ref _isDirect, value); } 
     } 
    } 
} 

和最後一兩件事:在你MainPage.xaml要綁定到rootPivot,所以你打電話

rootPivot.DataContext = null; 
rootPivot.DataContext = new MainPageViewModel(); 

或類似的東西。