C#学习教程:如何实现BoolToVisibilityConverter分享


如何实现BoolToVisibilityConverter

在我的应用程序中,我想切换StackPanel中项目的可见性。 我的Stackpanel包含一个Image和一个TextBlock。 我如何正确使用BoolToVisibilityConverter来切换TextBlock的可见性,并为用户的利益保存此设置?

目前我的情况如下,虽然我收到了一些错误。 重要提示,我需要使用ApplicationBar菜单项作为单击事件来驱动TextBox可见性的切换。

编辑

尽管TextBlock的可见性未发生变化,但不再出现错误。

XAML

xmlns:common="clr-namespace:TestApp.Common"                   

代码背后

 private void BuildLocalizedApplicationBar() { ApplicationBar = new ApplicationBar(); ApplicationBarMenuItem showFilterNamesMenuItem = new ApplicationBarMenuItem(); if (Settings.ShowFilterNames.Value) showFilterNamesMenuItem.Text = AppResources.EditPage_EffectNames_Hide; else showFilterNamesMenuItem.Text = AppResources.EditPage_EffectNames_Show; showFilterNamesMenuItem.Click += showFilterNamesMenuItem_Click; ApplicationBar.MenuItems.Add(showFilterNamesMenuItem); } void showFilterNamesMenuItem_Click(object sender, EventArgs e) { if(Settings.ShowFilterNames.Value) { ((ApplicationBarMenuItem)ApplicationBar.MenuItems[0]).Text = AppResources.EditPage_EffectNames_Hide; Settings.ShowFilterNames.Value = false; //Toggle the text block visibility to here } else { ((ApplicationBarMenuItem)ApplicationBar.MenuItems[0]).Text = AppResources.EditPage_EffectNames_Show; Settings.ShowFilterNames.Value = true; //Toggle the text block visibility to here } } 

BooleanToVisibilityConverter的类

 //Error on BooleanToVisibilityConverter stating does not implement interface member 'System.Windows.Data.IValueConverter.Convert(object, System.Type, object, System.Globalization.CultureInfo) public class BooleanToVisibilityConverter : IValueConverter { public class BooleanToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo language) { return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo language) { return value is Visibility && (Visibility)value == Visibility.Visible; } } 

试试这个:

 public class BooleanToVisibilityConverter : IValueConverter { private object GetVisibility(object value) { if (!(value is bool)) return Visibility.Collapsed; bool objValue = (bool)value; if (objValue) { return Visibility.Visible; } return Visibility.Collapsed; } public object Convert(object value, Type targetType, object parameter, string language) { return GetVisibility(value); } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } 

这是我的:

 public class BoolToVisConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, string language) { return value is Visibility && (Visibility)value == Visibility.Visible; } } 

已经有转换器的实现: http : //msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter(v=vs.110).aspx

上述就是C#学习教程:如何实现BoolToVisibilityConverter分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/1031761.html

发表评论

邮箱地址不会被公开。 必填项已用*标注