I have written a converter class inheriting from IValueConverter
. When I bind the cursor of a control to a CheckBox or ToggleButton's Ischecked
property using this converter, nothing happens. Here is my BooleanToCursorConverter
class:
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;
}
}
}
and here is my XAML code (simplified):
<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/>
You are returning the wrong type of value from your converter. The Cursor
property can only be set to a 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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.