今天積攢了一個轉(zhuǎn)換器的用法,分享給各位。
我們經(jīng)常會有這種需求: 某些控件有時需要顯示,有時需要隱藏,比如:
那,我就想通過一個bool變量和是否顯示綁定。
但是我們知道,是否顯示,這個屬性?Visibility 它并不是一個bool類型無法直接綁定,此時我們需要一個轉(zhuǎn)換器!
接下來,這個轉(zhuǎn)換器可以講bool類型轉(zhuǎn)換為字符串類型,十分通用,我們就已Visibility舉個例子:
<UserControl.Resources>
<wpfcv:Bool2StringConverter x:Key="b2s"/>
</UserControl.Resources>
------------------------------------------------------------------
Visibility="{Binding CanShowCross,
Converter={StaticResource b2s},
ConverterParameter=Visible:Hidden}"
?這里我們使用了?Bool2StringConverter? 這個轉(zhuǎn)換器,使用這個轉(zhuǎn)換器是,還給轉(zhuǎn)換器傳了一個參數(shù),可以將其看成一個字符串 " Visible:Hidden "
記下來我們看看?Bool2StringConverter? 是如何定義的。
后臺代碼
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace WpfConverter
{
public class Bool2StringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter == null) return DependencyProperty.UnsetValue;
if (value == null) return DependencyProperty.UnsetValue;
//將參數(shù)字符分段 parray[0]為比較值,parray[1]為true返回值,parray[2]為false返回值
string[] parray = parameter.ToString().ToLower().Split(':');
if (value is bool && parray.Length >=2)
{
//value為bool,true返回parray[1],false返回parray[2]
return (bool)value? parray[0] : parray[1];
}
else
{
return DependencyProperty.UnsetValue;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
//throw new NotImplementedException();
return DependencyProperty.UnsetValue;
}
}
}
在后臺中,我們根據(jù)綁定的bool值,來判斷返回,字符串?Visible:Hidden 的哪個部分,如果是true
就返回Visible, 否則返回Hidden。是不是很巧妙。
這樣我就可以通過這么一句話:
Visibility="{Binding CanShowCross,
Converter={StaticResource b2s},
ConverterParameter=Visible:Hidden}"
來實現(xiàn)某個控件的顯示與否了!
--------------2023年10月9日
根據(jù)網(wǎng)友weixin_42179035?的提問,我發(fā)現(xiàn)確實少了點內(nèi)容:補充內(nèi)容如下
private bool canShowCross;
/// <summary>
/// 十字線顯示
/// </summary>
public bool CanShowCross
{
get { return canShowCross; }
set { SetProperty(ref canShowCross, value); }
}
這里就是通過,CheekBox的IsCheeked屬性和 Line的?Visibility屬性,同時綁定了CanShowCross:
這樣當點擊CheekBox時,CanShowCross就會被改變,此時也就會改變Line的?Visibility屬性。文章來源:http://www.zghlxwxcb.cn/news/detail-425364.html
<Line X1="0" Stroke="Pink" Focusable="False" Visibility="{Binding CanShowCross,Converter={StaticResource b2s},ConverterParameter=Visible:Hidden}"
Y1="{Binding ActualHeight, Converter={StaticResource half}, ElementName=hSmart}"
X2="{Binding ActualWidth, ElementName=hSmart}"
Y2="{Binding ActualHeight, Converter={StaticResource half},ElementName=hSmart}" />
<Line X1="{Binding ActualWidth, Converter={StaticResource half},ElementName=hSmart}"
Y1="0" Stroke="Pink" Focusable="False" IsEnabled="False" Visibility="{Binding CanShowCross,Converter={StaticResource b2s},ConverterParameter=Visible:Hidden}"
X2="{Binding ActualWidth, Converter={StaticResource half},ElementName=hSmart}"
Y2="{Binding ElementName=hSmart, Path=ActualHeight}" />
<ToolBar Grid.ColumnSpan="2">
<CheckBox IsChecked="{Binding CanShowCross}" Margin="5,0,0,0">十字線</CheckBox>
</ToolBar>
(補充2)這里又引出了一個half轉(zhuǎn)換器,顧名思義就得到屬性值的一半大?。?span toymoban-style="hidden">文章來源地址http://www.zghlxwxcb.cn/news/detail-425364.html
public class HalfConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || (double)value == 0) return DependencyProperty.UnsetValue;
return (double)(value) / 2;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
//throw new NotImplementedException();
return DependencyProperty.UnsetValue;
}
}
到了這里,關(guān)于【wpf】轉(zhuǎn)換器 Converter的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!