Click here to Skip to main content
15,887,302 members
Home / Discussions / WPF
   

WPF

 
QuestionCustom FrameworkElement with child controls Pin
Kenneth Haugland12-Jan-16 7:51
mvaKenneth Haugland12-Jan-16 7:51 
AnswerRe: Custom FrameworkElement with child controls Pin
Gerry Schmitz12-Jan-16 17:06
mveGerry Schmitz12-Jan-16 17:06 
GeneralRe: Custom FrameworkElement with child controls Pin
Kenneth Haugland13-Jan-16 1:39
mvaKenneth Haugland13-Jan-16 1:39 
GeneralRe: Custom FrameworkElement with child controls Pin
Gerry Schmitz13-Jan-16 6:25
mveGerry Schmitz13-Jan-16 6:25 
QuestionBinding a CompositeCollection to a Canvas (C#) Pin
Kenneth Haugland8-Jan-16 22:31
mvaKenneth Haugland8-Jan-16 22:31 
AnswerRe: Binding a CompositeCollection to a Canvas (C#) Pin
Kenneth Haugland9-Jan-16 2:42
mvaKenneth Haugland9-Jan-16 2:42 
AnswerRe: Binding a CompositeCollection to a Canvas (C#) Pin
Richard Deeming11-Jan-16 3:51
mveRichard Deeming11-Jan-16 3:51 
GeneralRe: Binding a CompositeCollection to a Canvas (C#) Pin
Kenneth Haugland11-Jan-16 4:34
mvaKenneth Haugland11-Jan-16 4:34 
Ah, yes, I didn't like the static value or Dictionary either, so I ended up using a delegate instead:
C#
elements.CollectionChanged -= (sender, ee) => MyCollectionChangedHandler(cv, sender, ee);

        private static void MyCollectionChangedHandler(MyCanvas cv, object sender, NotifyCollectionChangedEventArgs e)

I also thought they static members were shared everywhere in the application, but I made the attached property code below:
C#
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.Input;

namespace MyApplication
{
    public static class TextBoxUpdateExplicitBinding
    {


        private static TextBox tb;
        private static List<TextBox> MyList;

        static TextBoxUpdateExplicitBinding()
        {
            MyList = new List<TextBox>();
        }

        #region "UpdateSourceOnKeyProperty"

        public static readonly DependencyProperty UpdateSourceOnKeyProperty =
           DependencyProperty.RegisterAttached(
               "UpdateSourceOnKey",
               typeof(Key),
               typeof(TextBoxUpdateExplicitBinding),
               new FrameworkPropertyMetadata(Key.None, KeyChanged));

        public static void SetUpdateSourceOnKey(UIElement element, Key value)
        {
            element.SetValue(UpdateSourceOnKeyProperty, value);
        }

        public static Key GetUpdateSourceOnKey(UIElement element)
        {
            return (Key)element.GetValue(UpdateSourceOnKeyProperty);
        }

        private static void KeyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            tb = (TextBox)obj;
            if (!MyList.Contains(tb))
            {
                MyList.Add(tb);
                tb.PreviewKeyDown += TextBoxKeyDown;
            }
        }
        #endregion

        static void TextBoxKeyDown(object sender, KeyEventArgs e)
        {
            tb = (TextBox)sender;
            if (tb == null) return;

            var propertyValue = (Key)tb.GetValue(UpdateSourceOnKeyProperty);
            if (e.Key != propertyValue) return;

            var bindingExpression = tb.GetBindingExpression(TextBox.TextProperty);
            if (bindingExpression != null) bindingExpression.UpdateSource();
        }

        public static DependencyProperty BindProperty = DependencyProperty
                        .RegisterAttached("Bind",
                        typeof(bool),
                        typeof(TextBoxUpdateExplicitBinding),
                        new PropertyMetadata(AttachCommand));

        public static void SetBind(DependencyObject d, bool command)
        {
            d.SetValue(BindProperty, command);
        }

        private static void AttachCommand(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            (d as Button).Click += Button_Click;
        }

        private static void Button_Click(object sender, RoutedEventArgs e)
        {
            var bindingExpression = tb.GetBindingExpression(TextBox.TextProperty);
            if (bindingExpression != null) bindingExpression.UpdateSource();
        }
    }
}

I now used this in a separate user contol:
HTML
<TextBox x:Name="tb" Grid.Column="0" local:TextBoxUpdateExplicitBinding.UpdateSourceOnKey="Return"
                   Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:SearchControl}},
                                  Path=SearchText,
                                  UpdateSourceTrigger=Explicit,
                                  Mode=OneWayToSource }"/>
          <Button  Grid.Column="1" local:TextBoxUpdateExplicitBinding.Bind="True" >
              <ContentControl>
                  <Image Source="pack://application:,,,/Icons/Search.png" Height="20" Width="20"/>
              </ContentControl>
          </Button>

And if I now added several of this user control in the same window, they didnt seem to share the static values in the attatched property. That's why I wondered.
QuestionCan I run UITests on a WPF application. Pin
James_Parsons8-Jan-16 4:56
James_Parsons8-Jan-16 4:56 
AnswerRe: Can I run UITests on a WPF application. Pin
NickPace8-Jan-16 5:06
NickPace8-Jan-16 5:06 
QuestionLabel does not Get Updated Pin
Vimalsoft(Pty) Ltd6-Jan-16 20:33
professionalVimalsoft(Pty) Ltd6-Jan-16 20:33 
AnswerRe: Label does not Get Updated Pin
Mycroft Holmes6-Jan-16 20:45
professionalMycroft Holmes6-Jan-16 20:45 
GeneralRe: Label does not Get Updated Pin
Vimalsoft(Pty) Ltd6-Jan-16 20:49
professionalVimalsoft(Pty) Ltd6-Jan-16 20:49 
GeneralRe: Label does not Get Updated Pin
Mycroft Holmes6-Jan-16 21:04
professionalMycroft Holmes6-Jan-16 21:04 
GeneralRe: Label does not Get Updated Pin
Vimalsoft(Pty) Ltd6-Jan-16 21:09
professionalVimalsoft(Pty) Ltd6-Jan-16 21:09 
QuestionWPF/MVVM Unit Test Questions Pin
Kevin Marois5-Jan-16 5:46
professionalKevin Marois5-Jan-16 5:46 
AnswerRe: WPF/MVVM Unit Test Questions Pin
Pete O'Hanlon5-Jan-16 5:58
mvePete O'Hanlon5-Jan-16 5:58 
GeneralRe: WPF/MVVM Unit Test Questions Pin
Kevin Marois5-Jan-16 6:02
professionalKevin Marois5-Jan-16 6:02 
GeneralRe: WPF/MVVM Unit Test Questions Pin
Pete O'Hanlon5-Jan-16 8:26
mvePete O'Hanlon5-Jan-16 8:26 
GeneralRe: WPF/MVVM Unit Test Questions Pin
Mycroft Holmes6-Jan-16 20:49
professionalMycroft Holmes6-Jan-16 20:49 
GeneralRe: WPF/MVVM Unit Test Questions Pin
Pete O'Hanlon6-Jan-16 21:06
mvePete O'Hanlon6-Jan-16 21:06 
GeneralRe: WPF/MVVM Unit Test Questions Pin
Mycroft Holmes7-Jan-16 12:03
professionalMycroft Holmes7-Jan-16 12:03 
GeneralRe: WPF/MVVM Unit Test Questions Pin
Pete O'Hanlon7-Jan-16 21:37
mvePete O'Hanlon7-Jan-16 21:37 
Questionwhat is the best way to save images to print in wpf Pin
neodeaths5-Jan-16 4:12
neodeaths5-Jan-16 4:12 
QuestionAdding icons to a ListView from an image list Pin
zahalul28-Dec-15 11:09
zahalul28-Dec-15 11:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.