Developer technologies | Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I'm building a simple user control toggle switch, I'm using the margin to move the Ellipse once the user selects with the left mouse button. I want the usage to bind to a boolean to make it simple.
The control works when i put it in a datagrid but fails to bind to the data source i provide (obviously) i dont know how to tie everything together
i added the final usage at the bottom
i tested the usage with just regular checkboxes in the datagrid and the bind works fine
can some body help
Thanks
Madaxe
<UserControl x:Class="WpfApp5.ToggleButtonUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp5"
mc:Ignorable="d"
d:DesignHeight="120" d:DesignWidth="240">
<Grid>
<Viewbox>
<Grid Height="120" Width="240" Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">
<Rectangle Name="Rec_Back"
MouseLeftButtonDown="Rec_Back_MouseLeftButtonDown"
Fill="#FFF54E42"
Height="115" Width="235"
RadiusY="60" RadiusX="60"
Margin="2.5,2.5,2.5,2.5"
HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Ellipse Name="Eli_Slider"
MouseLeftButtonDown="Eli_Slider_MouseLeftButtonDown"
Fill="White"
Height="110" Width="110"
Margin="{Binding Path=ThicknessBinding, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left" VerticalAlignment="Top" />
</Grid>
</Viewbox>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp5
{
/// <summary>
/// Interaction logic for ToggleButtonUserControl.xaml
/// </summary>
public partial class ToggleButtonUserControl : UserControl
{
public bool IsToggled {
get { return Convert.ToBoolean(this.GetValue(IsToggledProperty)); }
set { this.SetValue(IsToggledProperty, value); } }
public static readonly DependencyProperty IsToggledProperty =
DependencyProperty.Register("IsToggled", typeof(bool), typeof(ToggleButtonUserControl),
new PropertyMetadata(false, new PropertyChangedCallback(ChangeIsToggled)));
private static void ChangeIsToggled(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue == true)
{
(d as ToggleButtonUserControl).Eli_Slider.Margin = (d as ToggleButtonUserControl).LeftThickness;
}
else
{
(d as ToggleButtonUserControl).Eli_Slider.Margin = (d as ToggleButtonUserControl).RightThickness;
}
}
public Thickness ThicknessBinding {
get { return (Thickness)(this.GetValue(ThicknessBindingProperty)); }
set { this.SetValue(ThicknessBindingProperty, value); }}
public static readonly DependencyProperty ThicknessBindingProperty =
DependencyProperty.Register("ThicknessBinding", typeof(Thickness), typeof(ToggleButtonUserControl),
new PropertyMetadata(new Thickness(5, 5, 5, 5), new PropertyChangedCallback(CallbackThicknessBinding)));
private static void CallbackThicknessBinding(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
public Thickness LeftThickness = new Thickness(5, 5, 5, 5);
public Thickness RightThickness = new Thickness(125, 5, 5, 5);
public SolidColorBrush OffColor = new SolidColorBrush(Color.FromRgb(245, 78, 66));
public SolidColorBrush OnColor = new SolidColorBrush(Color.FromRgb(66, 245, 96));
public ToggleButtonUserControl()
{
InitializeComponent();
Eli_Slider.DataContext = this;
Rec_Back.Fill = this.OffColor;
this.IsToggled = false;
Eli_Slider.Margin = RightThickness;
}
private void Eli_Slider_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ToggleAction();
}
private void Rec_Back_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ToggleAction();
}
private void ToggleAction()
{
if (!this.IsToggled)
{
Eli_Slider.Margin = RightThickness;
this.IsToggled = true;
Rec_Back.Fill = this.OnColor;
}
else
{
Eli_Slider.Margin = LeftThickness;
this.IsToggled = false;
Rec_Back.Fill = this.OffColor;
}
}
}
}
<Window x:Class="WpfApp5.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:WpfApp5"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<DataGrid
Name="TestDataGrid"
ItemsSource="{Binding SecurityModels,
Mode=TwoWay,
NotifyOnSourceUpdated=True,
UpdateSourceTrigger=PropertyChanged}"
AlternatingRowBackground="Gray"
CanUserAddRows="False"
AutoGenerateColumns="False"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="400"
Width="619"
Margin="10,10,0,0" Grid.RowSpan="5">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding application_Name}" Header="Application Name"/>
<DataGridTemplateColumn Header="Can Create">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:ToggleButtonUserControl Width="44" Height="20" IsToggled="{Binding canCreate}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Can Delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:ToggleButtonUserControl Width="44" Height="20" IsToggled="{Binding canDelete}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Can Read">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:ToggleButtonUserControl Width="44" Height="20" IsToggled="{Binding canRead}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Can Replace">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:ToggleButtonUserControl Width="44" Height="20" IsToggled="{Binding canReplace}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Can Update">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:ToggleButtonUserControl Width="44" Height="20" IsToggled="{Binding canUpdate}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
I FIXED IT
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace WpfApp5
{
public partial class ToggleButtonUserControl : UserControl
{
public bool IsToggled
{
get { return (bool)GetValue(IsToggledProperty);}
set { SetValue(IsToggledProperty, value); Console.WriteLine(value); }
}
public static readonly DependencyProperty IsToggledProperty =
DependencyProperty.Register("IsToggled", typeof(bool), typeof(ToggleButtonUserControl),
new PropertyMetadata(false, new PropertyChangedCallback(ChangeIsToggled)));
private static void ChangeIsToggled(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//Console.WriteLine((d as ToggleButtonUserControl).IsToggled.ToString());
if ((bool)e.NewValue == false)
{
(d as ToggleButtonUserControl).Eli_Slider.Margin = (d as ToggleButtonUserControl).LeftThickness;
(d as ToggleButtonUserControl).Rec_Back.Fill = (d as ToggleButtonUserControl).OffColor;
}
else
{
(d as ToggleButtonUserControl).Eli_Slider.Margin = (d as ToggleButtonUserControl).RightThickness;
(d as ToggleButtonUserControl).Rec_Back.Fill = (d as ToggleButtonUserControl).OnColor;
}
}
public Thickness LeftThickness = new Thickness(5, 5, 5, 5);
public Thickness RightThickness = new Thickness(125, 5, 5, 5);
public SolidColorBrush OffColor = new SolidColorBrush(Color.FromRgb(245, 78, 66));
public SolidColorBrush OnColor = new SolidColorBrush(Color.FromRgb(66, 245, 96));
public ToggleButtonUserControl()
{
InitializeComponent();
}
private void Eli_Slider_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.IsToggled = (this.IsToggled) ? false : true;
}
private void Rec_Back_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.IsToggled = (this.IsToggled) ? false : true;
}
}
}