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();
}
}
}
您是否嘗試過做一個CLR命名空間聲明,而不是一個簡單的使用? 相反的: 的xmlns:轉換器= 「使用:Test_Tool.Common」 用途: 的xmlns:轉換器= 「CLR的命名空間:Test_Tool.Common」 – exceptionthrown
@exceptionthrown可惜的是沒有解決它。該複選框仍然可見,並且我的轉換器中的斷點從未被擊中 – Kyle
您可能需要指定哪些屬性已更改。而不是:OnPropertyChanged()使用:OnPropertyChanged(nameof(IsDirect))。 – exceptionthrown