我会学习在Wpf(xaml)上使用转换器。
<Window x:Class="TextExpanderGriglia.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TextExpanderGriglia"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
<local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
</Window.Resources>
<Grid>
<Button Content="CIAO" Width="50" Height="50" Visibility="{Binding vButton,Converter={StaticResource BoolToVisibilityConverter}}"> </Button>
<Button Content="Cambia" Width="50" Height="50" Margin="56,134,411,135" Click="Button_Click"/>
</Grid>
这是我的Xaml代码。 愚蠢的例子开始,我有2个按钮和按钮&#34;坎比亚&#34;我设置了布尔值vButton =!vButton,但如果vButton为false,则第一个按钮不会隐藏。 我的代码中缺少什么?
这是我的转换器
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Hidden;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return (Visibility)value == Visibility.Visible;
}
}
MainWindows.xaml.cs
public partial class MainWindow : Window, INotifyPropertyChanged
{
private bool vButton;
public event PropertyChangedEventHandler PropertyChanged;
public bool VButton
{
get
{
return vButton;
}
set
{
if (value != vButton)
{
this.vButton = value;
NotifyPropertyChanged("VButton");
}
}
}
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
vButton = !vButton;
}
}
答案 0 :(得分:1)
转换器看起来很好。
此处最有可能的情况是您绑定的属性不会引发更改通知。例如:
using System.ComponentModel;
using System.Runtime.CompilerServices;
public class MyModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _isButtonVisible;
public bool vButton
{
get { return _isButtonVisible; }
set
{
if (value == _isButtonVisible)
return;
_isButtonVisible = value;
OnPropertyChanged();
}
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
答案 1 :(得分:0)
设置DataContext
在你的构造函数中:
height
在MVVM模式中,您应该与Windows分开定义ViewModel。 对于前者在你的情况下:
@IBOutlet weak var content: UIView!
@IBAction func indexChanged(_ sender: UISegmentedControl)
{
if let vc = getViewController(sender.selectedSegmentIndex) {
self.addChildViewController(vc)
self.transition(from: self.currentViewController!,
to: vc,
duration: 0.5,
options: UIViewAnimationOptions.transitionCrossDissolve,
animations: {
self.currentViewController!.view.removeFromSuperview()
vc.view.frame = self.content.bounds
self.content.addSubview(vc.view)
},
completion: { finished in
self.currentViewController!.removeFromParentViewController()
self.currentViewController = vc
})
}
}
然后在你的MainWindow.xaml.cs
中public MainWindow()
{
InitializeComponent();
DataContext = this;
}