[英]Boolean to Cursor Converter in Xaml
我寫了一個繼承自IValueConverter
的轉換器類。 當我使用此轉換器將控件的光標綁定到CheckBox或ToggleButton的Ischecked
屬性時,什么也沒有發生。 這是我的BooleanToCursorConverter
類:
using System;
using System.Windows.Data;
using System.Windows.Input;
namespace Machine_Vision
{
[ValueConversion(typeof(bool?), typeof(CursorType))]
public class BooleanToCursorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool?)value==true)
{
return CursorType.Cross;
}
else
{
return CursorType.Arrow;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
}
這是我的XAML代碼(簡體):
<Window x:Class="Machine_Vision.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:Machine_Vision"
mc:Ignorable="d"
Title="MainWindow"
Height="650"
Width="1300"
WindowStartupLocation="CenterScreen"
WindowState="Maximized"
Closing="Window_Closing">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="B2V" />
<local:BooleanToCursorConverter x:Key="C2V" />
</Window.Resources>
<Grid>
<Image x:Name="Original_View"
Cursor="{Binding IsChecked, Converter={StaticResource C2V}, ElementName=DrawPoints}" />
<ToggleButton x:Name="DrawPoints"
Content="Draw Points"
Margin="5" />
<Grid/>
您從轉換器返回了錯誤的值類型。 Cursor
屬性只能設置為System.Windows.Input.Cursor
:
[ValueConversion(typeof(bool?), typeof(Cursor))]
public class BooleanToCursorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool?)value == true)
{
return Cursors.Cross;
}
else
{
return Cursors.Arrow;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.