Usercontrol not showing in designer - c#
I have a custom user control:
<UserControl Name="ddTextBox" x:Class="UserControlsLibrary.DDTextBox"
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"
mc:Ignorable="d"
d:DesignWidth="250" FontFamily="Tahoma" FontSize="14" MinHeight="25" Height="275">
<StackPanel>
<TextBox Name="ControlText" Height="25" HorizontalAlignment="Stretch" VerticalAlignment="Top" Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<ListBox Name="ControlList" VerticalAlignment="Top" MaxHeight="250" HorizontalAlignment="Stretch" Visibility="Collapsed" ItemsSource="{Binding Path=DList, UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</UserControl>
With the code behind:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace UserControlsLibrary
{
public partial class DDTextBox : UserControl, INotifyPropertyChanged
{
private string _text;
public string Text { get => _text ?? string.Empty; set { _text = value; OnPropertyChanged(); } }
public DDTextBox()
{
InitializeComponent();
ControlText.DataContext = this;
ControlList.DataContext = this;
ControlText.TextChanged += TextChanged;
ControlText.GotKeyboardFocus += KeyboardFocusChanged;
ControlText.LostKeyboardFocus += KeyboardFocusChanged;
ControlList.SelectionChanged += SelectionChanged;
ControlList.GotKeyboardFocus += KeyboardFocusChanged;
ControlList.LostKeyboardFocus += KeyboardFocusChanged;
IsKeyboardFocusWithinChanged += DDTextBox_IsKeyboardFocusWithinChanged;
}
private void TextChanged(object sender, TextChangedEventArgs e)
{ DItemsSource = ItemsSource.Cast<string>().ToList().FindAll(r => r.IndexOf(( (TextBox)sender ).Text, StringComparison.OrdinalIgnoreCase) >= 0); }
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{ Text = ( e.AddedItems.Count > 0 ) ? e.AddedItems[0].ToString() : string.Empty; ( (ListBox)sender ).Visibility = Visibility.Collapsed; }
private void KeyboardFocusChanged(object sender, KeyboardFocusChangedEventArgs e)
{
ControlList.Visibility = ( e.NewFocus == ControlList || e.NewFocus == ControlText ) ? Visibility.Visible : Visibility.Collapsed;
if ( e.OldFocus?.GetType() == typeof(ListBox) ) ( (ListBox)e.OldFocus ).SelectedIndex = -1;
}
private void DDTextBox_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if ( (bool)e.OldValue == true && (bool)e.NewValue == false )
{ RaiseEvent(new RoutedEventArgs(UserControlLostFocusEvent, this)); }
}
private void ControlLostFocus(object sender, RoutedEventArgs e)
{ if ( !ControlList.IsFocused && !ControlText.IsFocused ) { ControlList.Visibility = Visibility.Collapsed; ControlList.SelectedIndex = -1; } }
public event RoutedEventHandler UserControlLostFocus
{
add { AddHandler(UserControlLostFocusEvent, value); }
remove { RemoveHandler(UserControlLostFocusEvent, value); }
}
public IEnumerable ItemsSource
{
private get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); SetValue(DItemsSourceProperty, value); }
}
/// <summary>
/// Dynamically generated ItemsSource based on Text property. Changes made to this may be lost. List changes should be made to the ItemsSource.
/// </summary>
public IEnumerable DItemsSource
{
private get { return (IEnumerable)GetValue(DItemsSourceProperty); }
set { SetValue(DItemsSourceProperty, value); }
}
public static DependencyProperty DItemsSourceProperty => dItemsSourceProperty;
public static DependencyProperty ItemsSourceProperty => itemsSourceProperty;
public static RoutedEvent UserControlLostFocusEvent => userControlLostFocusEvent;
private static readonly DependencyProperty itemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(DDTextBox), new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged)));
private static readonly DependencyProperty dItemsSourceProperty = DependencyProperty.Register("DItemsSource", typeof(IEnumerable), typeof(DDTextBox), new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged)));
private static readonly RoutedEvent userControlLostFocusEvent = EventManager.RegisterRoutedEvent("UserControlLostFocus", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(DDTextBox));
private static void OnItemsSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{ if ( sender is DDTextBox control ) control.OnItemsSourceChanged((IEnumerable)e.NewValue, (IEnumerable)e.OldValue); }
private void OnItemsSourceChanged(IEnumerable newValue, IEnumerable oldValue)
{
if ( oldValue is INotifyCollectionChanged oldValueINotifyCollectionChanged )
{ oldValueINotifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(NewValueINotifyCollectionChanged_CollectionChanged); }
if ( newValue is INotifyCollectionChanged newValueINotifyCollectionChanged )
{ newValueINotifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(NewValueINotifyCollectionChanged_CollectionChanged); }
}
void NewValueINotifyCollectionChanged_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string Name = "")
{ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Name)); }
}
}
Referenced in my main project and added to the main window:
<Window Name="Main" x:Class="POSystem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myCtrls="clr-namespace:UserControlsLibrary;assembly=UserControlsLibrary"
Title="Purchase Order" Height="850" Width="1100" Background="#FFD8D0A9">
<Grid Name="TheGrid" Focusable="True" MouseDown="ClearFocus_OnClick" Background="Transparent">
<myCtrls:DDTextBox x:Name="ItemsBox" Width="300" VerticalAlignment="Top" HorizontalAlignment="Left" Panel.ZIndex="1"/>
</Grid>
</Window>
At runtime everything shows up and works as it is supposed too, however, in the window designer of the main page it does not show anything other than an outline indicating that the control is there. This outline can range from a single line at the location (only way all the functionality works proper) or if I add a minimum or just straight height it shows an empty outline of the control. How do I get this to show the (supposed to be) visible at all times textbox within the user control in the main window designer?
Related
UserControl in WinUI 3: how to set the 'source' property of an image and the 'click' event of a button?
I'm building a UserControl that should display a button with an image and text. I access that UserControl in the App like this: <local:ButtonWithImage ButtonClick="Button1_Click" ButtonImage="Assets/Clipboard 4.png" ButtonText="Clipboard History" ButtonWidth="200" /> Out of the 4 properties displayed in the code above, two of them are working fine, which are ButtonText and ButtonWidth. But the ButtonClick and ButtonImage properties are causing errors, which I'll explain next. The UserControl code is this: xaml: <UserControl x:Class="Launcher_WinUI3_Net_6.ButtonWithImage" 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:local="using:Launcher_WinUI3_Net_6" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel> <StackPanel Orientation="Horizontal"> <Button x:Name="button"> <StackPanel Orientation="Horizontal"> <Image x:Name="image"/> <TextBlock x:Name="textBlock" /> </StackPanel> </Button> </StackPanel> <TextBlock Height="1" /> </StackPanel> </UserControl> C#: public sealed partial class ButtonWithImage : UserControl { public ButtonWithImage() { this.InitializeComponent(); } public string ButtonText { get { return (string)GetValue(ButtonTextProperty); } set { SetValue(ButtonTextProperty, value); } } public static readonly DependencyProperty ButtonTextProperty = DependencyProperty.Register("ButtonText", typeof(string), typeof(ButtonWithImage), new PropertyMetadata(string.Empty, ButtonTextValue)); private static void ButtonTextValue(DependencyObject d, DependencyPropertyChangedEventArgs e) { var buttonWithImage = d as ButtonWithImage; var buttonWithImageProperty = buttonWithImage.FindName("textBlock") as TextBlock; buttonWithImageProperty.Text = e.NewValue.ToString(); } public string ButtonWidth { get { return (string)GetValue(ButtonWidthProperty); } set { SetValue(ButtonWidthProperty, value); } } public static readonly DependencyProperty ButtonWidthProperty = DependencyProperty.Register("ButtonWidth", typeof(string), typeof(ButtonWithImage), new PropertyMetadata(string.Empty, ButtonWidthValue)); private static void ButtonWidthValue(DependencyObject d, DependencyPropertyChangedEventArgs e) { var buttonWithImage = d as ButtonWithImage; var buttonWithImageProperty = buttonWithImage.FindName("button") as Button; buttonWithImageProperty.Width = Convert.ToDouble(e.NewValue.ToString()); } public string ButtonClick { get { return (string)GetValue(ButtonClickProperty); } set { SetValue(ButtonClickProperty, value); } } public static readonly DependencyProperty ButtonClickProperty = DependencyProperty.Register("ButtonClick", typeof(string), typeof(ButtonWithImage), new PropertyMetadata(string.Empty, ButtonClickValue)); private static void ButtonClickValue(DependencyObject d, DependencyPropertyChangedEventArgs e) { var buttonWithImage = d as ButtonWithImage; var buttonWithImageProperty = buttonWithImage.FindName("button") as Button; buttonWithImageProperty.Click += e.NewValue.ToString(); } public string ButtonImage { get { return (string)GetValue(ButtonImageProperty); } set { SetValue(ButtonImageProperty, value); } } public static readonly DependencyProperty ButtonImageProperty = DependencyProperty.Register("ButtonImage", typeof(string), typeof(ButtonWithImage), new PropertyMetadata(string.Empty, ButtonImageValue)); private static void ButtonImageValue(DependencyObject d, DependencyPropertyChangedEventArgs e) { var buttonWithImage = d as ButtonWithImage; var buttonWithImageProperty = buttonWithImage.FindName("image") as Image; buttonWithImageProperty.Source = e.NewValue.ToString(); } } The code for the ButtonClick is generating this error: Cannot implicitly convert type 'string' to 'Microsoft.UI.Xaml.RoutedEventHandler' And the code for the ButtonImage is generating this error: Cannot implicitly convert type 'string' to 'Microsoft.UI.Xaml.Media.ImageSource' I don't have much experience with creating UserControls so I'm following some examples I've seen on the internet, but none of them address these two problems I'm facing. ======================================================== Update 1 based on answer from Andrew KeepCoding: Thanks Andrew!!! There is still an error going on: No overload for 'Button52_Click' matches delegate 'EventHandler' UserControl in the App: <local:ButtonWithImage ButtonImage="Assets/Clipboard 4.png" ButtonText="Clipboard History" ButtonWidth="200" Click="Button52_Click" /> Button52_Click signature: private void Button52_Click(object sender, RoutedEventArgs e) { foo(); } UserControl 'Click' event signature: public event EventHandler? Click; private void button_Click(object sender, RoutedEventArgs e) { Click?.Invoke(this, EventArgs.Empty); } The signatures are the same, even so the error No overload for 'Button52_Click' matches delegate 'EventHandler' is occurring The error is occurring here, in 'case 41:': case 40: // MainWindow.xaml line 1288 { global::Microsoft.UI.Xaml.Controls.Button element40 = global::WinRT.CastExtensions.As<global::Microsoft.UI.Xaml.Controls.Button>(target); ((global::Microsoft.UI.Xaml.Controls.Button)element40).Click += this.Button51_Click; } break; case 41: // MainWindow.xaml line 1199 { global::Launcher_WinUI3_Net_6.ButtonWithImage element41 = global::WinRT.CastExtensions.As<global::Launcher_WinUI3_Net_6.ButtonWithImage>(target); ((global::Launcher_WinUI3_Net_6.ButtonWithImage)element41).Click += this.Button52_Click; } break; ======================================================== Update 2: The Button52_Click signature should be: private void Button52_Click(object sender, EventArgs e) { foo(); } And not: private void Button52_Click(object sender, RoutedEventArgs e) { foo(); }
Instead of typeof(string), you should use the actual type for your dependency properties. For example, I'm using ImageSource for the ButtonImage in the code below: <UserControl x:Class="UserControls.ButtonWithImage" 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" mc:Ignorable="d"> <StackPanel> <StackPanel Orientation="Horizontal"> <Button x:Name="button" Click="button_Click"> <StackPanel Orientation="Horizontal"> <Image x:Name="image" Source="{x:Bind ButtonImage, Mode=OneWay}" /> <TextBlock x:Name="textBlock" Text="{x:Bind ButtonText, Mode=OneWay}" /> </StackPanel> </Button> </StackPanel> <TextBlock Height="1" /> </StackPanel> </UserControl> using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; using System; namespace UserControls; public sealed partial class ButtonWithImage : UserControl { public static readonly DependencyProperty ButtonTextProperty = DependencyProperty.Register( nameof(ButtonText), typeof(string), typeof(ButtonWithImage), new PropertyMetadata(default)); public static readonly DependencyProperty ButtonImageProperty = DependencyProperty.Register( nameof(ButtonImage), typeof(ImageSource), typeof(ButtonWithImage), new PropertyMetadata(default)); public ButtonWithImage() { this.InitializeComponent(); } public event EventHandler? Click; public string ButtonText { get => (string)GetValue(ButtonTextProperty); set => SetValue(ButtonTextProperty, value); } public ImageSource? ButtonImage { get => (ImageSource?)GetValue(ButtonImageProperty); set => SetValue(ButtonImageProperty, value); } private void button_Click(object sender, RoutedEventArgs e) { Click?.Invoke(this, EventArgs.Empty); } } And use it like this: <local:ButtonWithImage ButtonText="Text" ButtonImage="Assets/StoreLogo.png" Click="ButtonWithImage_Click" /> private void ButtonWithImage_Click(object sender, EventArgs e) { } You should also consider a custom control derived from a Button. These videos might help. UserControls CustomControls
How to put a user control in a static layer ontop of all other controls?
I'm developing an autocomplete user control for WPF using XAML and C#. I would like to have the pop-up for the suggestions to appear above all controls. Currently my pop up is a ListView . That causes problems since whenever I decide to show it the UI must find a place for it and to do so moves all the controls which are below it further down. How can I avoid this? I assume I must put it in a layer which is above all of the other controls?
I have written "auto-complete" style controls before by using the WPF Popup control, combined with a textbox. If you use Popup it should appear, as you say, in a layer over the top of everything else. Just use Placement of Bottom to align it to the bottom of the textbox. Here is an example that I wrote a while ago. Basically it is a text box which, as you type pops up a suggestions popup, and as you type more it refines the options down. You could fairly easily change it to support multi-word auto-complete style code editing situations if you wanted that: XAML: <Grid> <TextBox x:Name="textBox" Text="{Binding Text, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:IntelliSenseUserControl}}}" KeyUp="textBox_KeyUp"/> <Popup x:Name="popup" Placement="Bottom" PlacementTarget="{Binding ElementName=textBox}" IsOpen="False" Width="200" Height="300"> <ListView x:Name="listView" ItemsSource="{Binding FilteredItemsSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:IntelliSenseUserControl}}}" SelectionChanged="ListView_Selected"/> </Popup> </Grid> Code-behind: public partial class IntelliSenseUserControl : UserControl, INotifyPropertyChanged { public IntelliSenseUserControl() { InitializeComponent(); DependencyPropertyDescriptor prop = DependencyPropertyDescriptor.FromProperty(ItemsSourceProperty, typeof(IntelliSenseUserControl)); prop.AddValueChanged(this, ItemsSourceChanged); } private void ItemsSourceChanged(object sender, EventArgs e) { FilteredItemsSource = new ListCollectionView((IList)ItemsSource); FilteredItemsSource.Filter = (arg) => { return arg == null || string.IsNullOrEmpty(textBox.Text) || arg.ToString().Contains(textBox.Text.Trim()); }; } public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(IntelliSenseUserControl), new FrameworkPropertyMetadata(null) { BindsTwoWayByDefault = true }); public object ItemsSource { get { return (object)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(object), typeof(IntelliSenseUserControl), new PropertyMetadata(null)); #region Notified Property - FilteredItemsSource (ListCollectionView) public ListCollectionView FilteredItemsSource { get { return filteredItemsSource; } set { filteredItemsSource = value; RaisePropertyChanged("FilteredItemsSource"); } } private ListCollectionView filteredItemsSource; #endregion private void textBox_KeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.Return || e.Key == Key.Enter) { popup.IsOpen = false; } else { popup.IsOpen = true; FilteredItemsSource.Refresh(); } } private void UserControl_LostFocus(object sender, RoutedEventArgs e) { popup.IsOpen = false; } private void ListView_Selected(object sender, RoutedEventArgs e) { if (listView.SelectedItem != null) { Text = listView.SelectedItem.ToString().Trim(); } } public event PropertyChangedEventHandler PropertyChanged; void RaisePropertyChanged(string name) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } }
If your Window's content container is a Grid, you can simply do something like <ListBox Grid.RowSpawn="99" Grid.ColumnSpan="99"/> to "simulate" an absolute position. You then just have to set its position with Margin, HorizontalAlignment and VerticalAlignment so it lays around the desired control.
Click-to-Edit Control LostFocus event issue
I am working on a simple Custom Control that should go to Edit mode by double clicking on it The concept is based on this question Click-to-edit in Silverlight On a double click it changes initial template on Edit Template and it seems to be pretty clear, except the part (5) How to change the template Back when the Control Looses the focus The Lost Focus event is fired only when contained controls are loosing focus Here is an article that talk about it http://programmerpayback.com/2008/11/20/gotfocus-and-lostfocus-events-on-containers/ I have tried to implement same Technic but still no result, I cannot get LostFocus event working for me when I click outside of a control Where is my issue? My XAML <ContentControl x:Class="Splan_RiaBusinessApplication.Controls.TimeCodeControl" 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:obj="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:behaviour="clr-namespace:Splan_RiaBusinessApplication.Behavior" xmlns:controls="clr-namespace:Splan_RiaBusinessApplication.Controls" xmlns:Primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data" mc:Ignorable="d" IsTabStop="True" IsEnabled="True" Visibility="Visible" d:DesignHeight="100" d:DesignWidth="200" d:Height="200" d:Width="200" > <ContentControl.Resources> <ControlTemplate x:Key="DisplayTemplate"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding Target.Code, Mode=TwoWay}" /> <StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" > <TextBlock Text="{Binding Target.Start, Mode=TwoWay, StringFormat=hh\\:mm }" /> <TextBlock Text='-' /> <TextBlock Text="{Binding Target.End, Mode=TwoWay, StringFormat=hh\\:mm }" /> </StackPanel> </Grid> </ControlTemplate> <ControlTemplate x:Key="EditTemplate"> <Grid Background="Aqua" Height="200" Width="200"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <ComboBox Width="100" Height="25" x:Name="cbTimeCode" ItemsSource="{Binding TimeCodes}" SelectedValue="{Binding Target.CodeId, Mode=TwoWay}" SelectedValuePath="TimeCodeId" > <ComboBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="40"/> <ColumnDefinition Width="100"/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Target.Code}" /> <TextBlock Grid.Column="1" Text="{Binding Target.Description}" /> </Grid> </DataTemplate> </ComboBox.ItemTemplate> <i:Interaction.Triggers> <i:EventTrigger> <behaviour:ResolveElementName PropertyName="ItemsSource" /> </i:EventTrigger> </i:Interaction.Triggers> </ComboBox> <!--<controls:TimeRangePickerControl Grid.Row="1" StartTime="{Binding Target.Start, Mode=TwoWay}" EndTime="{Binding Target.End, Mode=TwoWay}"/>--> </Grid> </ControlTemplate> </ContentControl.Resources> <Grid x:Name="Layout" Background="Aquamarine"> <ItemsControl x:Name="PlaceHolder" Template="{StaticResource DisplayTemplate}"> </ItemsControl> </Grid> </ContentControl> Code Behind using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace Splan_RiaBusinessApplication.Controls { public class TimeCode { public int TimeCodeId {get;set;} public string Code { get; set; } public string Description { get; set; } } public class TimeDetail { public int TimeDetailId { get;set; } public int CodeId { get;set;} public TimeSpan Start { get; set; } public TimeSpan End { get; set; } public string Code { get; set; } public string Comment { get; set; } } public partial class TimeCodeControl : ContentControl { public class TimeCodeControlEventArgs : EventArgs { public string userName { get; set; } } private static TimeSpan DoubleClickThreshold = TimeSpan.FromMilliseconds(300); private DateTime _lastClick; private Boolean m_EditMode = false; public Boolean EditMode { get { return m_EditMode; } set { if (m_EditMode != value) { switch (value) { case false: PlaceHolder.Template = this.Resources["DisplayTemplate"] as ControlTemplate; break; case true: PlaceHolder.Template = this.Resources["EditTemplate"] as ControlTemplate; break; } m_EditMode = value; } } } public bool IsFocused { get { return FocusManager.GetFocusedElement() == this; } } public TimeCodeControl() { TimeCodes = new List<TimeCode>() { new TimeCode { TimeCodeId = 200, Code= "C", Description="Cash" } }; InitializeComponent(); Layout.DataContext = this; this.IsTabStop = true; this.Visibility = Visibility.Visible; this.IsEnabled = true; this.Focus(); Layout.MouseLeftButtonDown += Layout_MouseLeftButtonDown; //Layout.KeyDown += Layout_KeyDown; //Layout.KeyUp += Layout_KeyUp; this.LostFocus += TimeCodeControl_LostFocus; this.GotFocus += TimeCodeControl_GotFocus; } void TimeCodeControl_GotFocus(object sender, RoutedEventArgs e) { } void TimeCodeControl_LostFocus(object sender, RoutedEventArgs e) { } public TimeDetail Source { get { return (TimeDetail)GetValue(SourceProperty); } set { SetValue(SourceProperty, value); } } public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(TimeDetail), typeof(TimeCodeControl), new PropertyMetadata(SourceChanged)); private static void SourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { var control = sender as TimeCodeControl; if (control == null) return; control.Target = control.Source; //.Copy(); } public List<TimeCode> TimeCodes { get; set; } public TimeDetail Target { get; set; } private bool FocusIsInside(object parent) { bool rs = false; dynamic oFocus = FocusManager.GetFocusedElement(); while (oFocus != null) try { if ((oFocus.GetType() == parent.GetType()) && (oFocus.Equals(this))) { rs = true; break; } else { oFocus = oFocus.Parent; } } catch { break; } return rs; } private Boolean hasFocus = false; protected override void OnGotFocus(RoutedEventArgs e) { base.OnGotFocus(e); if (!hasFocus) { hasFocus = true; Debug.WriteLine("Container Got Focus"); } } protected override void OnLostFocus(RoutedEventArgs e) { base.OnLostFocus(e); //if (!FocusIsInside(Layout)) //{ // hasFocus = false; // Debug.WriteLine("Container Lost Focus"); // EditMode = false; //} } void Layout_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (DateTime.Now - this._lastClick <= DoubleClickThreshold) { EditMode = true; this._lastClick = DateTime.Now; e.Handled = true; return; } this._lastClick = DateTime.Now; } } } UPDATE : I decided to utilize timers to identify a scenario when user brings a focus from outside of the container or just switches focus from one control to another inside of the container. it may be not the best solution but it seems to be working for now. I would appreciate any suggestions or recommendations on different approaches or implementations. public partial class MyControl: ContentControl { ... public event EventHandler<RoutedEventArgs> LostFocus; public event EventHandler<RoutedEventArgs> GotFocus; bool Focused = false; DispatcherTimer FocusTimer = null; protected override void OnGotFocus(RoutedEventArgs e) { base.OnGotFocus(e); if (Focused) return; Focused = true; // it focused from the outside of the control // becouse the timer wasn't initialised on the previous LostFocused event // generated by other control in the same ContentControl contaner if (FocusTimer == null) { if (GotFocus != null) GotFocus(e.OriginalSource, e); Debug.WriteLine("Got Focus "); return; } // It was switched from one hosted control to another one FocusTimer.Stop(); FocusTimer = null; } protected override void OnLostFocus(RoutedEventArgs e) { base.OnLostFocus(e); if (e.OriginalSource is ComboBox && FocusManager.GetFocusedElement() is ComboBoxItem) return; FocusTimer = new DispatcherTimer(); Focused = false; FocusTimer.Interval = new TimeSpan(0, 0, 0, 0, 50); FocusTimer.Tick += (s, args) => { FocusTimer.Stop(); FocusTimer = null; // if after the timeout the focus still not triggered // by another contained element // the We lost a focus on the container if (!Focused ) { if(LostFocus != null) LostFocus(e.OriginalSource, e); Debug.WriteLine("Lost Focus " ); } }; FocusTimer.Start(); } ... }
There are several issues. Let's see... Why are you not getting a LostFocus event when you click outside of the control? Well, I fell victim to this false assumption some time ago too. The click outside does not change the focus unless you click a control that explicitly sets focus to itself on click (like a TextBox does, or the various Buttons). Press Tab to navigate the keyboard focus to the next control and see if the event is raised. But let's talk about the other issues: ControlTemplate x:Key="DisplayTemplate" and ControlTemplate x:Key="EditTemplate" Using ControlTemplates this way is not recommended. Rather use DataTemplate and corresponding ContentPresenters. TimeCodeControl : ContentControl and x:Class="Splan_RiaBusinessApplication.Controls.TimeCodeControl" Yes I know that's possible, but not really useful. Let me explain: You can write your own specialized Click-To-Edit Control as a one-shot tool: having a hardcoded DisplayTemplate and EditTemplate to edit TimeCode and TimeDetail data (basically what you did). But then you have no chance of ever using it and specifying another pair of Templates to allow editing of other data types. So it doesn't make much sense to derive from ContentControl, you could as well derive from UserControl. An alternative would be: Write your Click-To-Edit control as a general and reusable control, that offers two public properties: DisplayTemplate and EditTemplate. And don't make any assumptions about your DataContext. And again there is no benefit from having ContentControl as the parent class. I recommend you derive from Control, add two DependencyProperties of type DataTemplate as mentioned earlier, define a default ControlTemplate with one or two ContentPresenters inside. In your control code you need to handle MouseLeftButtonDown and LostFocus and update a boolean flag accordingly. Here is a working example: ...extension method to determine focus: public static class ControlExtensions { public static bool IsFocused( this UIElement control ) { DependencyObject parent; for (DependencyObject potentialSubControl = FocusManager.GetFocusedElement() as DependencyObject; potentialSubControl != null; potentialSubControl = parent) { if (object.ReferenceEquals( potentialSubControl, control )) { return true; } parent = VisualTreeHelper.GetParent( potentialSubControl ); if (parent == null) { FrameworkElement element = potentialSubControl as FrameworkElement; if (element != null) { parent = element.Parent; } } } return false; } } ...and a nice custom control: public class ClickToEditControl : Control { public ClickToEditControl() { DefaultStyleKey = typeof (ClickToEditControl); MouseLeftButtonDown += OnMouseLeftButtonDown; } private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.ClickCount==2) { GotoEditMode(); e.Handled = true; } } protected override void OnLostFocus(RoutedEventArgs e) { base.OnLostFocus(e); if (!this.IsFocused()) GotoDisplayMode(); } private void GotoDisplayMode() { IsInEditMode = false; } private void GotoEditMode() { IsInEditMode = true; } public DataTemplate EditTemplate { get { return (DataTemplate) GetValue( EditTemplateProperty ); } set { SetValue( EditTemplateProperty, value ); } } public static readonly DependencyProperty EditTemplateProperty = DependencyProperty.Register( "EditTemplate", typeof( DataTemplate ), typeof( ClickToEditControl ), null ); public DataTemplate DisplayTemplate { get { return (DataTemplate) GetValue( DisplayTemplateProperty ); } set { SetValue( DisplayTemplateProperty, value ); } } public static readonly DependencyProperty DisplayTemplateProperty = DependencyProperty.Register( "DisplayTemplate", typeof( DataTemplate ), typeof( ClickToEditControl ), null ); public bool IsInEditMode { get { return (bool) GetValue( IsInEditModeProperty ); } set { SetValue( IsInEditModeProperty, value ); } } public static readonly DependencyProperty IsInEditModeProperty = DependencyProperty.Register( "IsInEditMode", typeof( bool ), typeof( ClickToEditControl ), null ); } ...and ControlTemplate: <clickToEdit:BoolToVisibilityConverter x:Key="VisibleIfInEditMode"/> <clickToEdit:BoolToVisibilityConverter x:Key="CollapsedIfInEditMode" VisibleIfTrue="False"/> <Style TargetType="clickToEdit:ClickToEditControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="clickToEdit:ClickToEditControl"> <Grid> <ContentPresenter ContentTemplate="{TemplateBinding EditTemplate}" Content="{Binding}" Visibility="{Binding IsInEditMode, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource VisibleIfInEditMode}}"/> <ContentPresenter ContentTemplate="{TemplateBinding DisplayTemplate}" Content="{Binding}" Visibility="{Binding IsInEditMode, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource CollapsedIfInEditMode}}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> BoolToVisibilityConverter public class BoolToVisibilityConverter : IValueConverter { public bool VisibleIfTrue { get; set; } public BoolToVisibilityConverter(){VisibleIfTrue = true;} public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (VisibleIfTrue) return ((bool) value) ? Visibility.Visible : Visibility.Collapsed; else return ((bool) value) ? Visibility.Collapsed : Visibility.Visible; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){throw new NotSupportedException();} } Usage: <clickToEdit:ClickToEditControl Height="20" Width="200"> <clickToEdit:ClickToEditControl.DisplayTemplate> <DataTemplate> <TextBlock Text="{Binding MyText}"/> </DataTemplate> </clickToEdit:ClickToEditControl.DisplayTemplate> <clickToEdit:ClickToEditControl.EditTemplate> <DataTemplate> <TextBox Text="{Binding MyText, Mode=TwoWay}"/> </DataTemplate> </clickToEdit:ClickToEditControl.EditTemplate> </clickToEdit:ClickToEditControl>
I can't Data Bind to a local variable in WPF/XAML
I want a textbox to display the value of a variable when I click it (an iteration of 1 to 100), I do not know what I am doing Wrong: When I run the project nothing is displayed in the text box. What is the best way to display variables in a text box? using System; using System.Collections.Generic; using System.Linq; using System.Text; 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 dataBindingTest { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public string myText { get; set; } public void Button_Click_1(object sender, RoutedEventArgs e) { int i = 0; for (i = 0; i < 100; i++) { myText = i.ToString(); } } } } XAML: <Window x:Class="dataBindingTest.MainWindow" Name="windowElement" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Button Content="Button" HorizontalAlignment="Left" Height="106" Margin="71,95,0,0" VerticalAlignment="Top" Width="125" Click="Button_Click_1"/> <TextBlock x:Name="myTextBox" HorizontalAlignment="Left" Height="106" Margin="270,95,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="187" Text= "{Binding myText, ElementName=windowElement}" /> </Grid> </Window>
Your current myText property has no way of notifying the WPF binding system when its value has changed, so the TextBlock wont be updated. If you make it a dependency property instead it automatically implements change notification, and the changes to the property will be reflected in the TextBlock. So if you replace public string myText { get; set; } with all of this code it should work: public string myText { get { return (string)GetValue(myTextProperty); } set { SetValue(myTextProperty, value); } } // Using a DependencyProperty as the backing store for myText. This enables animation, styling, binding, etc... public static readonly DependencyProperty myTextProperty = DependencyProperty.Register("myText", typeof(string), typeof(Window1), new PropertyMetadata(null));
implement INotifyPropertyChanged: public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { this.InitializeComponent(); } private string _txt; public string txt { get { return _txt; } set { if (_txt != value) { _txt = value; OnPropertyChanged("txt"); } } } private void Button_Click(object sender, RoutedEventArgs e) { txt = "changed text"; } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } XAML: <TextBox Text="{Binding txt}"/> <Button Click="Button_Click">yes</Button> and don't forget about adding the DataContext property of your window: <Window ... DataContext="{Binding RelativeSource={RelativeSource Self}}"/>
Try this: public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); this.DataContext = this; } public string myText { get; set; } public void Button_Click_1(object sender, RoutedEventArgs e) { BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += delegate { int i = 0; for (i = 0; i < 100; i++) { System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((Action)(() => { myText = i.ToString(); OnPropertyChanged("myText"); })); Thread.Sleep(100); } }; bw.RunWorkerAsync(); } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } } XAML file: <Grid> <Button Content="Button" HorizontalAlignment="Left" Height="106" Margin="71,95,0,0" VerticalAlignment="Top" Width="125" Click="Button_Click_1"/> <TextBlock x:Name="myTextBox" HorizontalAlignment="Right" Height="106" Margin="0,95,46,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="187" Text= "{Binding myText}" /> </Grid>
You should implement INotifyPropertyChanged in your "MainWindow" so your "myTextBlock" can automatically pick up changes from your data and update. So your "MainWindow" should look like: public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); } private string _myText; public string myText { get{return _myText;} set{_myText = value; if(PropertyChanged!=null) PropertyChanged(this, new PropertyChangedEventArgs("myText")) ; } } public event PropertyChangedEventHandler PropertyChanged; etc..... }
You need to make the property tell the binding that it has updated. The standard way to do this is via: Implementing INotifyPropertyChanged Making the myText property a DependencyProperty Another maybe less used way is to raise the event manually, like this: public void Button_Click_1(object sender, RoutedEventArgs e) { myText = "Clicked"; BindingOperations.GetBindingExpressionBase(myTextBox, TextBlock.TextProperty).UpdateTarget(); } Note that your TextBlock has the confusing name myTextBox
Unable to set property in usercontrol from viewmodel
I am trying to set a property in a usercontrol from a view model, the control is meant to capture and display the users signature. I am having problems setting the property in the usercontrol from a view model. Receiving the signature from the usercontrol into the view model works fine. Any ideas on how to solve this would be of greatly appreciated. My test code is below. User control XAML: <UserControl x:Class="mvvmSignature.ucSignature" 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" mc:Ignorable="d" d:DesignHeight="212" d:DesignWidth="300" Background="#FFEFFCEF"> <Grid Height="210"> <InkCanvas MinHeight="73" HorizontalAlignment="Left" Margin="10,27,0,0" Name="inkCanvas1" VerticalAlignment="Top" MinWidth="278" LostMouseCapture="inkCanvas1_LostMouseCapture" /> <Button Content="Clear" Height="23" HorizontalAlignment="Left" Margin="33,142,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="buttonClear_Click" /> <Button Content="Load on user control" Height="23" Margin="143,143,36,45" Click="ButtonLoad_Click" /> </Grid> </UserControl> UserControl CodeBehind: using System.IO; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Ink; using System.Windows.Input; namespace mvvmSignature { public partial class ucSignature : UserControl { public ucSignature() { InitializeComponent(); } private void inkCanvas1_LostMouseCapture(object sender, MouseEventArgs e) { SetSignature(); } private void SetSignature() { var sb = new StringBuilder(); using (var ms = new MemoryStream()) { inkCanvas1.Strokes.Save(ms); foreach (var item in ms.ToArray()) { sb.AppendFormat("{0},", item); } ms.Close(); } var local = sb.ToString().Trim() + "¬¬¬"; local = local.Replace(",¬¬¬", string.Empty); Signature = local; } private void LoadSignature(string signatureIn) { if (string.IsNullOrEmpty(signatureIn) || !signatureIn.Contains(",")) return; var x = signatureIn.Split(',').Select(byte.Parse).ToArray(); if (!x.Any()) return; using (var ms = new MemoryStream(x)) { inkCanvas1.Strokes = new StrokeCollection(ms); ms.Close(); } } public static DependencyProperty SignatureProperty = DependencyProperty.Register("Signature", typeof(string), typeof(ucSignature)); public string Signature { get { return (string) GetValue(SignatureProperty); } set { SetValue(SignatureProperty, value); LoadSignature(value); } } private void ButtonLoad_Click(object sender, RoutedEventArgs e) { Signature = "0,136,3,3,6,72,16,69,53,70,53,17,0,0,128,63,31,9,17,0,0,0,0,0,0,240,63,10,237,2,186,1,135,240,12,39,128,97,109,28,4,156,51,90,235,138,135,131,227,95,107,253,119,193,35,104,195,186,246,103,1,195,179,59,78,134,195,179,92,167,188,180,76,206,211,192,77,175,108,211,28,53,136,32,51,41,156,210,3,148,109,22,188,163,105,195,188,11,92,11,184,122,101,188,166,182,124,53,106,153,90,44,217,71,15,64,102,115,59,52,205,105,1,53,128,76,166,179,73,156,202,107,0,195,248,122,106,153,64,22,185,164,210,3,51,154,64,102,51,8,5,174,105,52,128,204,102,19,57,156,215,41,90,69,162,103,1,128,76,128,19,40,4,6,1,102,153,205,96,22,105,156,206,101,53,180,89,230,83,88,4,202,107,0,179,38,120,122,215,52,195,248,120,179,101,25,158,80,195,179,41,156,210,107,50,180,205,38,144,25,156,214,105,52,153,97,249,165,166,101,52,180,205,32,48,0,135,231,164,231,113,153,131,40,225,168,4,6,1,1,128,89,160,22,105,164,214,207,135,32,56,110,3,52,153,192,96,19,80,0,11,68,6,1,105,11,76,2,215,135,50,142,28,77,48,253,160,180,225,232,12,2,107,52,128,205,38,115,91,68,214,105,51,153,77,112,238,30,77,102,80,9,148,214,1,1,154,76,230,64,38,80,8,12,2,101,50,180,205,11,70,29,128,225,236,63,0,153,77,96,19,9,141,154,207,102,153,204,230,19,24,12,209,105,154,64,96,19,89,148,204,0,76,166,118,121,132,204,77,38,179,56,13,170,3,51,128,64,109,24,126,104,154,32,54,121,148,202,103,52,153,77,96,9,144"; } private void buttonClear_Click(object sender, RoutedEventArgs e) { inkCanvas1.Strokes.Clear(); SetSignature(); } } } MainWindow XAML: <Window xmlns:my="clr-namespace:mvvmSignature" x:Class="mvvmSignature.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:ignore="http://www.ignore.com" mc:Ignorable="d ignore" Height="490" Width="327" DataContext="{Binding Main, Source={StaticResource Locator}}"> <StackPanel> <my:ucSignature Signature ="{Binding Signature,Mode=TwoWay}" /> <Button Width ="140" Height ="30" Content ="Load On Main Page" Command="{Binding LoadOnMainPageCommand}" /> <TextBox FontSize="8" Foreground="Purple" Text="{Binding Signature,Mode=TwoWay}" VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap" Height="210" /> </StackPanel> MainViewModel: using System.Windows.Input; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; namespace mvvmSignature.ViewModel { public class MainViewModel : ViewModelBase { public ICommand LoadOnMainPageCommand { get; set; } public const string WelcomeTitlePropertyName = "Signature"; private string _signature = string.Empty; public string Signature { get { return _signature; } set { if (_signature == value) { return; } _signature = value; RaisePropertyChanged(WelcomeTitlePropertyName); } } public MainViewModel() { LoadOnMainPageCommand = new RelayCommand(LoadSignature); } private void LoadSignature() { Signature ="0,136,3,3,6,72,16,69,53,70,53,17,0,0,128,63,31,9,17,0,0,0,0,0,0,240,63,10,237,2,186,1,135,240,12,39,128,97,109,28,4,156,51,90,235,138,135,131,227,95,107,253,119,193,35,104,195,186,246,103,1,195,179,59,78,134,195,179,92,167,188,180,76,206,211,192,77,175,108,211,28,53,136,32,51,41,156,210,3,148,109,22,188,163,105,195,188,11,92,11,184,122,101,188,166,182,124,53,106,153,90,44,217,71,15,64,102,115,59,52,205,105,1,53,128,76,166,179,73,156,202,107,0,195,248,122,106,153,64,22,185,164,210,3,51,154,64,102,51,8,5,174,105,52,128,204,102,19,57,156,215,41,90,69,162,103,1,128,76,128,19,40,4,6,1,102,153,205,96,22,105,156,206,101,53,180,89,230,83,88,4,202,107,0,179,38,120,122,215,52,195,248,120,179,101,25,158,80,195,179,41,156,210,107,50,180,205,38,144,25,156,214,105,52,153,97,249,165,166,101,52,180,205,32,48,0,135,231,164,231,113,153,131,40,225,168,4,6,1,1,128,89,160,22,105,164,214,207,135,32,56,110,3,52,153,192,96,19,80,0,11,68,6,1,105,11,76,2,215,135,50,142,28,77,48,253,160,180,225,232,12,2,107,52,128,205,38,115,91,68,214,105,51,153,77,112,238,30,77,102,80,9,148,214,1,1,154,76,230,64,38,80,8,12,2,101,50,180,205,11,70,29,128,225,236,63,0,153,77,96,19,9,141,154,207,102,153,204,230,19,24,12,209,105,154,64,96,19,89,148,204,0,76,166,118,121,132,204,77,38,179,56,13,170,3,51,128,64,109,24,126,104,154,32,54,121,148,202,103,52,153,77,96,9,144"; } } }
I re-wrote portions of your ucSignature class to properly declare and use the Signature property in your code behind. Use this class definition instead, and let me know if it works any better. public partial class ucSignature : UserControl { public ucSignature() { InitializeComponent(); } private void inkCanvas1_LostMouseCapture(object sender, MouseEventArgs e) { var sb = new StringBuilder(); using (var ms = new MemoryStream()) { inkCanvas1.Strokes.Save(ms); foreach (var item in ms.ToArray()) { sb.AppendFormat("{0},", item); } ms.Close(); } var local = sb.ToString().Trim() + "¬¬¬"; local = local.Replace(",¬¬¬", string.Empty); this.SetValue(SignatureProperty, local); } private void LoadSignature(string signatureIn) { if (string.IsNullOrEmpty(signatureIn) || !signatureIn.Contains(",")) return; var x = signatureIn.Split(',').Select(byte.Parse).ToArray(); if (!x.Any()) return; using (var ms = new MemoryStream(x)) { inkCanvas1.Strokes = new StrokeCollection(ms); ms.Close(); } } public static DependencyProperty SignatureProperty = DependencyProperty.Register( "Signature", typeof(string), typeof(ucSignature), new UIPropertyMetadata(OnSignaturePropertyChanged)); public static string GetSignature(ucSignature signature) { return (string)signature.GetValue(SignatureProperty); } public static void SetSignature(ucSignature signature, string value) { signature.SetValue(SignatureProperty, value); } private static void OnSignaturePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { var signature = obj as ucSignature; if (signature != null) { LoadSignature(args.NewValue.ToString()); } } private void ButtonLoad_Click(object sender, RoutedEventArgs e) { this.SetValue(SignatureProperty, "0,136,3,3,6,72,16,69,53,70,53,17,0,0,128,63,31,9,17,0,0,0,0,0,0,240,63,10,237,2,186,1,135,240,12,39,128,97,109,28,4,156,51,90,235,138,135,131,227,95,107,253,119,193,35,104,195,186,246,103,1,195,179,59,78,134,195,179,92,167,188,180,76,206,211,192,77,175,108,211,28,53,136,32,51,41,156,210,3,148,109,22,188,163,105,195,188,11,92,11,184,122,101,188,166,182,124,53,106,153,90,44,217,71,15,64,102,115,59,52,205,105,1,53,128,76,166,179,73,156,202,107,0,195,248,122,106,153,64,22,185,164,210,3,51,154,64,102,51,8,5,174,105,52,128,204,102,19,57,156,215,41,90,69,162,103,1,128,76,128,19,40,4,6,1,102,153,205,96,22,105,156,206,101,53,180,89,230,83,88,4,202,107,0,179,38,120,122,215,52,195,248,120,179,101,25,158,80,195,179,41,156,210,107,50,180,205,38,144,25,156,214,105,52,153,97,249,165,166,101,52,180,205,32,48,0,135,231,164,231,113,153,131,40,225,168,4,6,1,1,128,89,160,22,105,164,214,207,135,32,56,110,3,52,153,192,96,19,80,0,11,68,6,1,105,11,76,2,215,135,50,142,28,77,48,253,160,180,225,232,12,2,107,52,128,205,38,115,91,68,214,105,51,153,77,112,238,30,77,102,80,9,148,214,1,1,154,76,230,64,38,80,8,12,2,101,50,180,205,11,70,29,128,225,236,63,0,153,77,96,19,9,141,154,207,102,153,204,230,19,24,12,209,105,154,64,96,19,89,148,204,0,76,166,118,121,132,204,77,38,179,56,13,170,3,51,128,64,109,24,126,104,154,32,54,121,148,202,103,52,153,77,96,9,144"); } private void buttonClear_Click(object sender, RoutedEventArgs e) { inkCanvas1.Strokes.Clear(); this.SetValue(SignatureProperty, string.Empty); } }
You need to set your MainViewModel as the DataContext of your MainWindow so the binding works. Unless you're setting it in the constructor, I don't see it being set anywhere. You can try something like this: <Application.Resources> <local:MainViewModel x:Key="MainViewModel" /> </Application.Resources> DataContext="{StaticResource MainViewModel}"
I'm going to assume that it's the Signature property that you are trying to set, mainly because that's what your user control is all about AND that's where I see an issue. After you set the Signature property, you call RaisePropertyChanged on a different property: the WelcomeTitlePropertyName property. You should be calling RaisePropertyChanged on the same property you set or else your UI (i.e. your user control) won't know about it. For example, in your MainWindow XAML you placed a ucSignature element like this: <my:ucSignature Signature ="{Binding Signature,Mode=TwoWay}" /> That part is fine, but in your ViewModel that property should be defined like this: private string _signature = string.Empty; public string Signature { get { return _signature; } set { if (_signature == value) return; _signature = value; RaisePropertyChanged(Signature); } }