I have this code:
<Rectangle x:Name="cage" Height="247" Canvas.Left="278" Canvas.Top="220" Width="450">
<Rectangle.Fill>
<ImageBrush Stretch="None" ImageSource="resources/cage.gif"/>
</Rectangle.Fill>
</Rectangle>
and
<Label x:Name="to1" Content="1" Canvas.Left="550" Canvas.Top="{Binding Source=cage, Path=Canvas.Top, Mode=OneWay}" BorderBrush="#FF272727" BorderThickness="1" Height="38" Width="38" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="20" PreviewMouseDown="to1_PreviewMouseDown"/>
Binding is working, but I want to maintain locations of this element relative to each other. Another words, if i shift the cage, topProperty of label became "shifting value + 285". How to do this?
Consider using a converter for this purpose as bit suggested.
A simple example should look like this:
XAML
<UserControl.Resources>
<converterNamespace:LabelOffsetConverter x:Key="labelOffsetConverter"/>
</UserControl.Resources>
<Label x:Name="to1" Content="1" Canvas.Left="550"
Canvas.Top="{Binding Source=cage, Path=Canvas.Top, Mode=OneWay,
Converter={StaticResource labelOffsetConverter}"
BorderBrush="#FF272727"
BorderThickness="1"
Height="38" Width="38"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
FontSize="20" PreviewMouseDown="to1_PreviewMouseDown"/>
Converter Code
public class LabelOffsetConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is double))
return DependencyProperty.UnsetValue;
double doubleValue = (double)value;
return doubleValue + 285;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// No need to implement, since the binding is in one way mode.
throw new NotImplementedException();
}
}
Solved by this way:
C#
public class Label1offset : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is double))
return DependencyProperty.UnsetValue;
double doubleValue = (double)value;
return doubleValue + 65.0;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// No need to implement, since the binding is in one way mode.
throw new NotImplementedException();
}
}
Binding binding1 = new Binding();
binding1.Converter = new Label1offset();
binding1.Source = cage;
binding1.Path = new PropertyPath(Canvas.TopProperty);
binding1.Mode = BindingMode.OneWay;
to1.SetBinding(Canvas.TopProperty, binding1);
Related
I want to search in multiple fields in AutoCompleteBox, I want to search in ClientName if the user starts typing characters, and search in ClientNumber if the user starts typing numbers.
I searched on the internet and from this Answer I understand that I should use Converter to accomplish what I want, But unfortunately, they do not explain how can I write the converter!
This is the AutoCompleteBox
<toolkit:AutoCompleteBox x:Name="txbClientName" FilterMode="StartsWith" IsTextCompletionEnabled="True"
ItemsSource="{Binding ocClients}"
ValueMemberBinding="{Binding Converter= {StaticResource ClientSearch}}"
SelectedItem="{Binding ElementName=this,
Path=ContactPerson,
Mode=TwoWay,
UpdateSourceTrigger=LostFocus}" PreviewKeyUp="txbClientName_PreviewKeyUp" LostFocus="txbClientName_LostFocus">
<toolkit:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding ContactPerson}"/>
</StackPanel>
</DataTemplate>
</toolkit:AutoCompleteBox.ItemTemplate>
What should the ClientSearch Converter do ??
public class ClientSearchConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//What should I write here !!!
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
Any help, please!
I have the following xaml:
<Border x:Name="brdImg3" BorderThickness="2" BorderBrush="Black" Margin="10,5,5,5" Cursor="Hand">
<Image x:Name="image3" Stretch="Fill" Cursor="Hand"/>
</Border>
The problem is, that the Hand-Cursor is only shown when the Source of Image is not null. When the Source is null, the Hand-Cursor is only shown when the Mouse is over the border. I need showing the Hand-Cursor when the Mouse is in the Border. How can I do this?
Use Converter in Cursor, check sources is null or empty. Logic is writen
Cursor={Binding Source,Converter={StaticResources CursorConverter}
public class CursorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!string.IsNullorEmpty(value.Tostring()))
{
return Cursor.Hand;
}
else
{
return Cursor.Arrow;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
I have a collection of Checkboxes in a Listbox binding to an Enum which uses a converter. Code shown below:
<ListBox HorizontalAlignment="Left" Height="183" Margin="159,30,0,0" VerticalAlignment="Top" Width="144">
<ListBox.Resources>
<local:FlagsEnumValueConverter x:Key="FlagsConverter" />
</ListBox.Resources>
<CheckBox Content="Checkbox1" IsChecked="{
Binding Path=TestModel.TestEnum,
Converter={StaticResource FlagsConverter},
ConverterParameter={x:Static tc:TestEnum.Test1}}"/>
<CheckBox Content="Checkbox2" IsChecked="{
Binding Path=TestModel.TestEnum,
Converter={StaticResource FlagsConverter},
ConverterParameter={x:Static tc:TestEnum.Test2}}"/>
<CheckBox Content="Checkbox3" IsChecked="{
Binding Path=TestModel.TestEnum,
Converter={StaticResource FlagsConverter},
ConverterParameter={x:Static tc:TestEnum.Test3}}"/>
</ListBox>
Converter Code:
class FlagsEnumValueConverter : IValueConverter
{
private int mTargetValue;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var mask = (int)parameter;
mTargetValue = (int)value;
return ((mask & mTargetValue) != 0);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
mTargetValue ^= (int)parameter;
return Enum.Parse(targetType, mTargetValue.ToString());
}
}
I'd rather use an ItemTemplate to keep things clean and simple however I'm not sure what to do with the 'ConverterParameter' since it requires the specific Enum value. I've been Googling for a solution but haven't found one. I honestly don't even know where to start. Is this even possible?
I'm trying to use a Converter inside a ResourceDictionary. That's the code I have:
<Window x:Class="Metro.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cnv="clr-namespace:Metro.converters">
<Window.Resources>
<cnv:DarkenColorConverter x:Key="Darken" />
<Color x:Key="Red">#FF0000</Color>
<SolidColorBrush Color="{StaticResource Red}"
x:Key="Accent" />
<SolidColorBrush Color="{Binding Source={StaticResource Red}, Converter={StaticResource ResourceKey=Darken}}"
x:Key="DarkAccent" />
</Window.Resources>
<StackPanel>
<Grid Background="{StaticResource Accent}">
<TextBlock>grid 1</TextBlock>
</Grid>
<Grid Background="{StaticResource DarkAccent}">
<TextBlock>grid 2</TextBlock>
</Grid>
</StackPanel>
</Window>
Here's the converter:
public class DarkenColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Brushes.Blue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Brushes.Gray;
}
}
But somehow it's not working. As soon as I use the converter inside the Grid directly, everything works fine:
<Grid Background="{Binding Source={StaticResource Red}, Converter={StaticResource ResourceKey=Darken}}">
<TextBlock>grid 2</TextBlock>
</Grid>
What`s wrong with the first xaml sample?
In the first conversion you are converting a Color, the one in the Grid is converting a SolidColorBrush.
You will have to modify your converter to accept Color also.
public class DarkenColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double percentage = 0.8;
if (parameter != null)
{
double.TryParse(parameter.ToString(), out percentage);
}
if (value is SolidColorBrush)
{
Color color = (value as SolidColorBrush).Color;
return new SolidColorBrush(Color.FromRgb((byte)(color.R * percentage), (byte)(color.G * percentage), (byte)(color.B * percentage)));
}
else if (value is Color)
{
Color color = (Color)value;
return Color.FromRgb((byte)(color.R * percentage), (byte)(color.G * percentage), (byte)(color.B * percentage));
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
The problem was the wrong converter return type.
Working converter:
public class DarkenColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Colors.Blue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Colors.Gray;
}
}
Unknown attribute Foreground on
element TextBlock
I have this error, when I'm trying to
change the Foreground color depending
on "Read_State"
public class ReadConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool ReadState =(bool)parameter;
if (ReadState == false)
return new SolidColorBrush(Colors.Black);// new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]);
else
return new SolidColorBrush(Colors.Black);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
In Xaml
<TextBlock Foreground="{Binding Converter={StaticResource ReadConverter},ConverterParameter={Binding Read_State}}" Text="{Binding Path=TexT}" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap"/>
The error may just be a bit misleading. You can't use Binding on the ConverterParameter.
You are miss using the converter, you don't need the ConverterParameter at all. Your converter code ought to look like this:-
public class ReadConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool ReadState =(bool)value;
if (ReadState == false)
return new SolidColorBrush(Colors.Black);// new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]);
else
return new SolidColorBrush(Colors.Black);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
and your Xaml:-
<TextBlock Foreground="{Binding Read_State, Converter={StaticResource ReadConverter}}" Text="{Binding Path=TexT}" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap"/>
You might also want to have a read of this blog for future use.