Asked by:
C# WPF user Control Binding Problem

Question
-
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>
As Busy As A Bricky In Beirut
- Edited by Madaxe Wednesday, April 8, 2020 8:07 PM
- Moved by Kyle Wang - MSFTMicrosoft contingent staff Thursday, April 9, 2020 1:13 AM
Wednesday, April 8, 2020 8:05 PM
All replies
-
Hi Madaxe,
Based on your description, it is related to WPF. So it is recommended to ask the questions in this forum and you can get more professional answer.
Thank you for your understanding.
Best Regards,
Daniel ZhangMSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Thursday, April 9, 2020 1:11 AM -
Hi Madaxe,
Same thing I wrote to you in your other thread (in case you forgot about this thread and/or for others reading this thread):
The MSDN forums are gradually being migrated to a new platform, called Microsoft Q&A. I don't know what the schedule is as to which forums get migrated when.
The "old" MSDN WPF Forum is here:
https://social.msdn.microsoft.com/Forums/en-US/home?forum=wpf&filter=alllanguages
As you can see, it has been archived, but is still available for looking at old posts (but not replying to or posting any new questions).
So, that said, here is where any new WPF questions get posted now (login with the same username/password that you use here in the MSDN forums):
https://docs.microsoft.com/answers/topics/wpf.html
~~Bonnie DeWitt [C# MVP]
Sunday, April 12, 2020 11:45 PM