How to prevent breaking line in ribbonButton label - c#

I have following ribbonButton in my wpf application
<RibbonButton Style="{StaticResource MenuButton}"
Label="{x:Static res:Resources.SaveAs}"
LargeImageSource="..\Images\saveas.png" />
It looks:saveAs with line break
but I need something like:saveAs without line break
I used non breaking space in xaml :
<RibbonButton Style="{StaticResource MenuButton}"
Label="Zapisz jako"
LargeImageSource="..\Images\saveas.png" />
But I also need that string from resx file because of localization. But   doesn't work when it is in resx. Is there some solution to put non breaking space into resx? Or should I modify ribbonButton template?

I did a behavior that does the job for me, perhaps it could works for you?
(it seems that you get the exact same problem as I did...)
Usage:
<RibbonButton Command="macro:MacroCommands.CommandMacroSaveAs" Label="Save as"
SmallImageSource="pack://application:,,,/WpfUtil;component/Images/document-save-as16.png"
LargeImageSource="pack://application:,,,/WpfUtil;component/Images/document-save-as32.png"
RibbonTwoLineText.HasTwoLines="False"
>
<i:Interaction.Behaviors>
<behaviors:BehaviorRibbonButton TextWrapping="NoWrap" />
</i:Interaction.Behaviors>
</RibbonButton>
Behavior:
using System.Windows;
using System.Windows.Controls.Ribbon;
using System.Windows.Data;
using System.Windows.Interactivity;
namespace HQ.Wpf.Util.Behaviors
{
public class BehaviorRibbonButton : Behavior<RibbonButton>
{
// ************************************************************************
public TextWrapping TextWrapping
{
get { return (TextWrapping)GetValue(TextWrappingProperty); }
set { SetValue(TextWrappingProperty, value); }
}
// ************************************************************************
public static readonly DependencyProperty TextWrappingProperty =
DependencyProperty.Register("TextWrapping", typeof(TextWrapping), typeof(BehaviorRibbonButton), new UIPropertyMetadata(TextWrapping.Wrap, TextWrappingPropertyChangedCallback));
// ************************************************************************
private static void TextWrappingPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var ribbonButton = dependencyObject as BehaviorRibbonButton;
if (ribbonButton != null)
{
ribbonButton.SetTextWrapping();
}
}
// ************************************************************************
public bool HasTwoLine
{
get
{
if (TextWrapping == TextWrapping.NoWrap)
{
return false;
}
return true;
}
}
// ************************************************************************
private void SetTextWrapping()
{
var ribbonButton = this.AssociatedObject as RibbonButton;
if (ribbonButton != null)
{
var ribbonTwoLineText = UiUtility.FindVisualChild<RibbonTwoLineText>(ribbonButton);
if (ribbonTwoLineText != null)
{
ribbonTwoLineText.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
var binding = new Binding("HasTwoLine");
binding.Source = this;
binding.Mode = BindingMode.OneWay;
ribbonTwoLineText.SetBinding(RibbonTwoLineText.HasTwoLinesProperty, binding);
}
}
}
// ************************************************************************
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.Loaded += AssociatedObjectOnLoaded;
}
// ************************************************************************
private void AssociatedObjectOnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
SetTextWrapping();
}
// ************************************************************************
}
}

Related

WPF . NET 3.5 C# Field is already declared / Member with the same name?

So, I corrected almost all my mistakes but here is one more important comes in if I can say.So I created a xaml named MessageBoxEx.xaml, with a "Loaded name: Window_Loaded", a textblock named "PartTextBlock" and a stackpanel named "PartStackPanel". Here is the xaml code shown above:
<Window x:Class="MyProject.MoLib.MessageBoxEx"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="Window_Loaded" Height="160" Width="440" WindowStyle="None" AllowsTransparency="true"
Background="Transparent" WindowStartupLocation="CenterOwner">
<Border CornerRadius="10" BorderBrush="{DynamicResource AccentBrush}" BorderThickness="3"
Background="{DynamicResource BackgroundBrush}">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" Height="112">
<TextBlock x:Name="PartTextBlock" Foreground="#000000" TextWrapping="Wrap"
Width="400" Height="Auto" Margin="20,34,10,6" TextAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
<StackPanel Name="PartStackPanel" Orientation="Horizontal" HorizontalAlignment="Right"/>
</StackPanel>
</Border>
I then put the names of these elements, so that they are referenced and realize a function, in MessageBoxEx.xaml.cs, here is the code:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
namespace MyProject.MoLib
{
public partial class MessageBoxEx : Window, IComponentConnector
{
private string FMessage = string.Empty;
private string FResult = (string)null;
private string FBtn0 = (string)null;
private string FBtn1 = (string)null;
private string FBtn2 = (string)null;
internal TextBlock PartTextBlock;
internal StackPanel PartStackPanel;
private bool _contentLoaded;
public string Btn0
{
get
{
return this.FBtn0;
}
set
{
this.FBtn0 = value;
}
}
public string Btn1
{
get
{
return this.FBtn1;
}
set
{
this.FBtn1 = value;
}
}
public string Btn2
{
get
{
return this.FBtn2;
}
set
{
this.FBtn2 = value;
}
}
public string Message
{
get
{
return this.FMessage;
}
set
{
this.FMessage = value;
}
}
public string Result
{
get
{
return this.FResult;
}
}
public TextBlock TextBlock
{
get
{
return this.PartTextBlock;
}
}
public MessageBoxEx()
{
this.InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (this.PartTextBlock.Inlines.Count < 1)
this.PartTextBlock.Text = this.FMessage;
this.SetupButton();
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
this.DragMove();
}
private void SetupButton()
{
if (this.FBtn0 != null)
this.CreateButton("btn1", this.FBtn0);
if (this.FBtn1 != null)
this.CreateButton("btn2", this.FBtn1);
if (this.FBtn2 != null)
this.CreateButton("btn3", this.FBtn2);
Border border = new Border();
border.Width = 10.0;
this.PartStackPanel.Children.Add((UIElement)border);
}
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button.Name == "btn1")
this.FResult = "0";
else if (button.Name == "btn2")
this.FResult = "1";
else if (button.Name == "btn3")
this.FResult = "2";
this.Close();
}
private void CreateButton(string name, string caption)
{
Button button = new Button();
button.Name = name;
button.Width = 80.0;
button.Content = (object)caption;
button.Margin = new Thickness(0.0, 15.0, 4.0, 0.0);
button.Click += new RoutedEventHandler(this.button_Click);
this.PartStackPanel.Children.Add((UIElement)button);
}
[DebuggerNonUserCode]
public void InitializeComponent()
{
if (this._contentLoaded)
return;
this._contentLoaded = true;
Application.LoadComponent((object)this, new Uri("/MoLib/MessageBoXex.xaml", UriKind.Relative));
}
[EditorBrowsable(EditorBrowsableState.Never)]
[DebuggerNonUserCode]
void IComponentConnector.Connect(int connectionId, object target)
{
switch (connectionId)
{
case 1:
((FrameworkElement)target).Loaded += new RoutedEventHandler(this.Window_Loaded);
break;
case 2:
this.PartTextBlock = (TextBlock)target;
break;
case 3:
this.PartStackPanel = (StackPanel)target;
break;
default:
this._contentLoaded = true;
break;
}
}
}
}
Hop, the names turn red and VS says to me: Ambiguous, already same name. Even for InitializeComponent(), IComponentConnector.Connect and _contentLoaded.
Apparently this error disappears when I change the name of the class but if I do that, the class of the .xaml becomes false.
I would also like to point out that if I change the name of the class, the object 'Loaded ="Window_Loaded" ' of the xaml is no longer referenced by the xaml.cs then it becomes red.
So, what could I do to correct that? Is it possible to be able to reference already existing name in MessageBoxEx.xaml in MessageBoxEx.xaml.cs? I also added the URI method for in case I have wrong to put the same names.
How do you proceed?
Get rid of the IComponentConnector interface and its members and don't define the fields that you that you define in your XAML markup in your code-behind file. These fields are generated automatically for you so your code-behind class should look like something like this:
public partial class MessageBoxEx : Window
{
private string FMessage = string.Empty;
private string FResult = (string)null;
private string FBtn0 = (string)null;
private string FBtn1 = (string)null;
private string FBtn2 = (string)null;
private bool _contentLoaded;
public string Btn0
{
get
{
return this.FBtn0;
}
set
{
this.FBtn0 = value;
}
}
public string Btn1
{
get
{
return this.FBtn1;
}
set
{
this.FBtn1 = value;
}
}
public string Btn2
{
get
{
return this.FBtn2;
}
set
{
this.FBtn2 = value;
}
}
public string Message
{
get
{
return this.FMessage;
}
set
{
this.FMessage = value;
}
}
public string Result
{
get
{
return this.FResult;
}
}
public TextBlock TextBlock
{
get
{
return this.PartTextBlock;
}
}
public MessageBoxEx()
{
this.InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (this.PartTextBlock.Inlines.Count < 1)
this.PartTextBlock.Text = this.FMessage;
this.SetupButton();
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
this.DragMove();
}
private void SetupButton()
{
if (this.FBtn0 != null)
this.CreateButton("btn1", this.FBtn0);
if (this.FBtn1 != null)
this.CreateButton("btn2", this.FBtn1);
if (this.FBtn2 != null)
this.CreateButton("btn3", this.FBtn2);
Border border = new Border();
border.Width = 10.0;
this.PartStackPanel.Children.Add((UIElement)border);
}
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button.Name == "btn1")
this.FResult = "0";
else if (button.Name == "btn2")
this.FResult = "1";
else if (button.Name == "btn3")
this.FResult = "2";
this.Close();
}
private void CreateButton(string name, string caption)
{
Button button = new Button();
button.Name = name;
button.Width = 80.0;
button.Content = (object)caption;
button.Margin = new Thickness(0.0, 15.0, 4.0, 0.0);
button.Click += new RoutedEventHandler(this.button_Click);
this.PartStackPanel.Children.Add((UIElement)button);
}
}

How to pass information from an attached behavior to the viewmodel with static events?

I have an attached property to textboxes in my view. The attached property performs validation on the textbox input and performs other chores. The attached property validation routine raises an event which is being watched by the viewmodel.
Does this "violate" MVVM reasoning by having the viewmodel obtain the invalid TextBoxes?
How will the GC deal with the static events from the attached property when the usercontrol containing the textboxes is removed?
If specific code is needed to avoid memory leaks, how is that done?
Is there a preferred way to do this?
Sorry for the long list, but Google does not address this situation.
Any and all help is appreciated. Thank you for your consideration.
(VS2010 .net 4.5)
TIA
ViewModel
class CheckInViewModel : SimpleViewModelBase
{
public CheckInViewModel()
{
InValidTextBoxes = new List<TextBox>();
Stargate_V.Helpers.ColorMaskingTextBoxBehavior.Validated += (sender, e) =>
{
if (e.valid)
InValidTextBoxes.Remove(e.sender);
else
InValidTextBoxes.Add(e.sender);
};
}
List<TextBox> InValidTextBoxes;
}
XAML
<TextBox
h:ColorMaskingTextBoxBehavior.Mask="^[MmFf]$"
Text="{Binding Sex}"
Height="24" HorizontalAlignment="Right" Margin="0,55,665,0" VerticalAlignment ="Top" Width="36" />
Attached Properity
public class ColorMaskingTextBoxBehavior : DependencyObject
{
// Entrance point from Xaml
public static readonly DependencyProperty MaskProperty = DependencyProperty.RegisterAttached("Mask",
typeof(string),
typeof(ColorMaskingTextBoxBehavior),
new FrameworkPropertyMetadata(OnMaskChanged));
...........................
// Callback from XAML initialization of the attached property.
private static void OnMaskChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var textBox = dependencyObject as TextBox;
var mask = e.NewValue as string;
textBox.PreviewTextInput -= textBox_PreviewTextInput;
textBox.PreviewKeyDown -= textBox_PreviewKeyDown;
DataObject.RemovePastingHandler(textBox, Pasting);
DataObject.RemoveCopyingHandler(textBox, NoDragCopy);
CommandManager.RemovePreviewExecutedHandler(textBox, NoCutting);
if (mask == null)
{
textBox.ClearValue(MaskProperty);
textBox.ClearValue(MaskExpressionProperty);
}
else
{
textBox.SetValue(MaskProperty, mask);
SetMaskExpression(textBox, new Regex(mask, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace));
textBox.PreviewTextInput += textBox_PreviewTextInput;
textBox.PreviewKeyDown += textBox_PreviewKeyDown;
DataObject.AddPastingHandler(textBox, Pasting);
DataObject.AddCopyingHandler(textBox, NoDragCopy);
CommandManager.AddPreviewExecutedHandler(textBox, NoCutting);
}
}
private static void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
var textBox = sender as TextBox;
var maskExpression = GetMaskExpression(textBox);
string passHex = (string)textBox.GetValue(PassColorProperty);
string failHex = (string)textBox.GetValue(FailColorProperty);
Color passColor = Extensions.ToColorFromHex(passHex);
Color failColor = Extensions.ToColorFromHex(failHex);
if (maskExpression == null)
{
return;
}
var proposedText = GetProposedText(textBox, e.Text);
if (!maskExpression.IsMatch(proposedText))
{
textBox.Background = new SolidColorBrush(failColor);
ValidationEventArgs args = new ValidationEventArgs();
args.sender = textBox;
args.valid = false;
OnValidation(args);
}
else
{
textBox.Background = new SolidColorBrush(passColor);
ValidationEventArgs args = new ValidationEventArgs();
args.sender = textBox;
args.valid = true;
OnValidation(args);
}
}
Event Called from the above code
public static event EventHandler<ValidationEventArgs> Validated;
static void OnValidation(ValidationEventArgs e)
{
EventHandler<ValidationEventArgs> handler = Validated;
if (handler != null)
{
handler(null, e);
}
}
public class ValidationEventArgs : EventArgs
{
public TextBox sender;
public bool valid;
}
Yes, I would argue that this violates MVVM. Your view model should have no knowledge of the views whatsoever. The question to always ask yourself is "can I run my application without creating any views?". In this case your view model is interacting directly with a list of TextBoxes, so the pattern is broken.
There are several ways of achieving your goal here, probably the most simple is to create a handler in your view model that gets called when your TextBox text changes:
public delegate void ValidationDelegate(bool isValid);
public class MyViewModel : ViewModelBase
{
public ValidationDelegate ValidationHandler { get { return (isValid) => OnValidate(isValid); } }
private void OnValidate(bool isValid)
{
// handle the validation event here
}
}
Now all you need is a behavior with an attached property that you can bind to this handler:
public class ValidateBehavior : Behavior<TextBox>
{
public ValidationDelegate Validated
{
get { return (ValidationDelegate)GetValue(ValidatedProperty); }
set { SetValue(ValidatedProperty, value); }
}
public static readonly DependencyProperty ValidatedProperty =
DependencyProperty.Register("Validated", typeof(ValidationDelegate), typeof(ValidateBehavior), new PropertyMetadata(null));
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.TextChanged += ValidateText;
}
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.TextChanged -= ValidateText;
}
private void ValidateText(object sender, TextChangedEventArgs e)
{
if (this.Validated != null)
{
bool isValid = true; // do text validation here
this.Validated(isValid);
}
}
}
And then finally add the behaviour to the TextBox in question and bind the handler:
<TextBox>
<i:Interaction.Behaviors>
<behaviors:ValidateBehavior Validated="{Binding ValidationHandler}"/>
</i:Interaction.Behaviors>
</TextBox>
EDIT: If you don't want to use a Blend behaviour then you can also do it with an attached behaviour:
public static class ValidateBehavior
{
public static ValidationDelegate GetValidate(TextBox textbox)
{
return (ValidationDelegate)textbox.GetValue(ValidateProperty);
}
public static void SetValidate(TextBox textbox, ValidationDelegate value)
{
textbox.SetValue(ValidateProperty, value);
}
public static readonly DependencyProperty ValidateProperty =
DependencyProperty.RegisterAttached(
"Validate",
typeof(ValidationDelegate),
typeof(ValidateBehavior),
new UIPropertyMetadata(null, OnValidateChanged));
static void OnValidateChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
var textbox = depObj as TextBox;
if (textbox == null)
return;
if (e.OldValue is ValidationDelegate)
textbox.TextChanged -= OnTextChanged;
if (e.NewValue is ValidationDelegate)
textbox.TextChanged += OnTextChanged;
}
static void OnTextChanged(object sender, RoutedEventArgs e)
{
if (!Object.ReferenceEquals(sender, e.OriginalSource))
return;
var textbox = e.OriginalSource as TextBox;
if (textbox != null)
{
var validate = GetValidate(textbox);
if (validate != null)
{
bool isValid = true; // do text validation here
validate(isValid);
}
}
}
}
And the corresponding XAML:
<TextBox behaviors:ValidateBehavior.Validate="{Binding ValidationHandler}" />

Access XAML object in ViewModel

How could I access a XAML object in my ViewModel? I am really confused. I want to access the <Controls:ModalContentPresenter> object. How could I realise this in a MVVM conform way? On this object I want to call a method ShowModalContent
<Controls:ModalContentPresenter x:Name="modalContent">
<ScrollViewer Behaviors:AdvancedZooming.KeepInCenter="true" Visibility="{Binding LeerformularIsVisible}" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Viewbox Stretch="Uniform">
<Grid>
<DataGrid BorderBrush="{x:Null}">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Command="{Binding AddFieldDefinitionCommand}" Header="Feld hinterlegen" Icon="pack://application:,,,/Images/Designer/field.png" />
<MenuItem Command="{Binding AddFunctionCommand}" Header="Funktion hinterlegen" Icon="pack://application:,,,/Images/Designer/FI_Taschenmesser_16x16.png" />
<MenuItem Command="{Binding RemoveFieldDefinitionCommand}" Header="Aktuelle Felddefinition entfernen" Icon="pack://application:,,,/Images/Designer/remove_field.png" />
<MenuItem Command="{Binding CutCommand}" Header="Ausschneiden" Icon="pack://application:,,,/Images/Zwischenablage/FI_Ausschneiden_16x16.png" />
<MenuItem Command="{Binding CopyCommand}" Header="Kopieren" Icon="pack://application:,,,/Images/Zwischenablage/FI_Kopieren_16x16.png" />
<MenuItem Command="{Binding PasteCommand}" Header="Einfügen" Icon="pack://application:,,,/Images/Zwischenablage/FI_Einfuegen_16x16.png" />
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
</Grid>
</Viewbox>
</ScrollViewer>
<Controls:ModalContentPresenter.ModalContent>
<StackPanel>
<TextBlock>Test</TextBlock>
<Button>Hide</Button>
</StackPanel>
</Controls:ModalContentPresenter.ModalContent>
</Controls:ModalContentPresenter>
The code of ModalContentPresenter can be found here:
using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
namespace Controls
{
[ContentProperty("Content")]
public class ModalContentPresenter : FrameworkElement
{
#region private fields
private Panel layoutRoot;
private ContentPresenter primaryContentPresenter;
private ContentPresenter modalContentPresenter;
private Border overlay;
private object[] logicalChildren;
private KeyboardNavigationMode cachedKeyboardNavigationMode;
private static readonly TraversalRequest traversalDirection;
#endregion
#region dependency properties
public static readonly DependencyProperty IsModalProperty = DependencyProperty.Register("IsModal", typeof(bool), typeof(ModalContentPresenter),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsModalChanged));
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(object), typeof(ModalContentPresenter),
new UIPropertyMetadata(null, OnContentChanged));
public static readonly DependencyProperty ModalContentProperty = DependencyProperty.Register("ModalContent", typeof(object), typeof(ModalContentPresenter),
new UIPropertyMetadata(null, OnModalContentChanged));
public static readonly DependencyProperty OverlayBrushProperty = DependencyProperty.Register("OverlayBrush", typeof(Brush), typeof(ModalContentPresenter),
new UIPropertyMetadata(new SolidColorBrush(Color.FromArgb(204, 169, 169, 169)), OnOverlayBrushChanged));
public bool IsModal
{
get { return (bool)GetValue(IsModalProperty); }
set { SetValue(IsModalProperty, value); }
}
public object Content
{
get { return (object)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
public object ModalContent
{
get { return (object)GetValue(ModalContentProperty); }
set { SetValue(ModalContentProperty, value); }
}
public Brush OverlayBrush
{
get { return (Brush)GetValue(OverlayBrushProperty); }
set { SetValue(OverlayBrushProperty, value); }
}
#endregion
#region routed events
public static readonly RoutedEvent PreviewModalContentShownEvent = EventManager.RegisterRoutedEvent("PreviewModalContentShown", RoutingStrategy.Tunnel,
typeof(RoutedEventArgs), typeof(ModalContentPresenter));
public static readonly RoutedEvent ModalContentShownEvent = EventManager.RegisterRoutedEvent("ModalContentShown", RoutingStrategy.Bubble,
typeof(RoutedEventArgs), typeof(ModalContentPresenter));
public static readonly RoutedEvent PreviewModalContentHiddenEvent = EventManager.RegisterRoutedEvent("PreviewModalContentHidden", RoutingStrategy.Tunnel,
typeof(RoutedEventArgs), typeof(ModalContentPresenter));
public static readonly RoutedEvent ModalContentHiddenEvent = EventManager.RegisterRoutedEvent("ModalContentHidden", RoutingStrategy.Bubble,
typeof(RoutedEventArgs), typeof(ModalContentPresenter));
public event RoutedEventHandler PreviewModalContentShown
{
add { AddHandler(PreviewModalContentShownEvent, value); }
remove { RemoveHandler(PreviewModalContentShownEvent, value); }
}
public event RoutedEventHandler ModalContentShown
{
add { AddHandler(ModalContentShownEvent, value); }
remove { RemoveHandler(ModalContentShownEvent, value); }
}
public event RoutedEventHandler PreviewModalContentHidden
{
add { AddHandler(PreviewModalContentHiddenEvent, value); }
remove { RemoveHandler(PreviewModalContentHiddenEvent, value); }
}
public event RoutedEventHandler ModalContentHidden
{
add { AddHandler(ModalContentHiddenEvent, value); }
remove { RemoveHandler(ModalContentHiddenEvent, value); }
}
#endregion
#region ModalContentPresenter implementation
static ModalContentPresenter()
{
traversalDirection = new TraversalRequest(FocusNavigationDirection.First);
}
public ModalContentPresenter()
{
layoutRoot = new ModalContentPresenterPanel();
primaryContentPresenter = new ContentPresenter();
modalContentPresenter = new ContentPresenter();
overlay = new Border();
AddVisualChild(layoutRoot);
logicalChildren = new object[2];
overlay.Background = OverlayBrush;
overlay.Child = modalContentPresenter;
overlay.Visibility = Visibility.Hidden;
layoutRoot.Children.Add(primaryContentPresenter);
layoutRoot.Children.Add(overlay);
}
public void ShowModalContent()
{
if (!IsModal)
IsModal = true;
}
public void HideModalContent()
{
if (IsModal)
IsModal = false;
}
private void RaiseModalContentShownEvents()
{
RoutedEventArgs args = new RoutedEventArgs(PreviewModalContentShownEvent);
OnPreviewModalContentShown(args);
if (!args.Handled)
{
args = new RoutedEventArgs(ModalContentShownEvent);
OnModalContentShown(args);
}
}
private void RaiseModalContentHiddenEvents()
{
RoutedEventArgs args = new RoutedEventArgs(PreviewModalContentHiddenEvent);
OnPreviewModalContentHidden(args);
if (!args.Handled)
{
args = new RoutedEventArgs(ModalContentHiddenEvent);
OnModalContentHidden(args);
}
}
protected virtual void OnPreviewModalContentShown(RoutedEventArgs e)
{
RaiseEvent(e);
}
protected virtual void OnModalContentShown(RoutedEventArgs e)
{
RaiseEvent(e);
}
protected virtual void OnPreviewModalContentHidden(RoutedEventArgs e)
{
RaiseEvent(e);
}
protected virtual void OnModalContentHidden(RoutedEventArgs e)
{
RaiseEvent(e);
}
#endregion
#region property changed callbacks
private static void OnIsModalChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ModalContentPresenter control = (ModalContentPresenter)d;
if ((bool)e.NewValue == true)
{
control.cachedKeyboardNavigationMode = KeyboardNavigation.GetTabNavigation(control.primaryContentPresenter);
KeyboardNavigation.SetTabNavigation(control.primaryContentPresenter, KeyboardNavigationMode.None);
control.overlay.Visibility = Visibility.Visible;
control.overlay.MoveFocus(traversalDirection);
control.RaiseModalContentShownEvents();
}
else
{
control.overlay.Visibility = Visibility.Hidden;
KeyboardNavigation.SetTabNavigation(control.primaryContentPresenter, control.cachedKeyboardNavigationMode);
control.primaryContentPresenter.MoveFocus(traversalDirection);
control.RaiseModalContentHiddenEvents();
}
}
private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ModalContentPresenter control = (ModalContentPresenter)d;
if (e.OldValue != null)
control.RemoveLogicalChild(e.OldValue);
control.primaryContentPresenter.Content = e.NewValue;
control.AddLogicalChild(e.NewValue);
control.logicalChildren[0] = e.NewValue;
}
private static void OnModalContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ModalContentPresenter control = (ModalContentPresenter)d;
if (e.OldValue != null)
control.RemoveLogicalChild(e.OldValue);
control.modalContentPresenter.Content = e.NewValue;
control.AddLogicalChild(e.NewValue);
control.logicalChildren[1] = e.NewValue;
}
private static void OnOverlayBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ModalContentPresenter control = (ModalContentPresenter)d;
control.overlay.Background = (Brush)e.NewValue;
}
#endregion
#region FrameworkElement overrides
protected override Visual GetVisualChild(int index)
{
if (index < 0 || index > 1)
throw new ArgumentOutOfRangeException("index");
return layoutRoot;
}
protected override int VisualChildrenCount
{
get { return 1; }
}
protected override IEnumerator LogicalChildren
{
get { return logicalChildren.GetEnumerator(); }
}
protected override Size ArrangeOverride(Size finalSize)
{
layoutRoot.Arrange(new Rect(finalSize));
return finalSize;
}
protected override Size MeasureOverride(Size availableSize)
{
layoutRoot.Measure(availableSize);
return layoutRoot.DesiredSize;
}
#endregion
#region layout panel
class ModalContentPresenterPanel : Panel
{
protected override Size MeasureOverride(Size availableSize)
{
Size resultSize = new Size(0, 0);
foreach (UIElement child in Children)
{
child.Measure(availableSize);
resultSize.Width = Math.Max(resultSize.Width, child.DesiredSize.Width);
resultSize.Height = Math.Max(resultSize.Height, child.DesiredSize.Height);
}
return resultSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
foreach (UIElement child in InternalChildren)
{
child.Arrange(new Rect(finalSize));
}
return finalSize;
}
}
#endregion
}
}
You shouldn't do this (at least, this is definitely not a MVVM-way).
Use IsModal property and data binding instead:
<Controls:ModalContentPresenter x:Name="modalContent" IsModal="{Binding IsModal}">
where IsModal in the binding expression is a bound data property within your view model.
In MVVM, any data going back and forth should be done via data binding properties on your View Model. That includes virtually any properties of the ContentPresenter - just add a binding. However, if you want call a method of a XAML object in an MVVM-friendly way, you can use an Action.
Let's suppose I wanted to set the focus on a textbox via the view model (contrived example, I know there's other MVVM-ways to do this- just wanted an example that requires a child object of the view).
First, give the textbox that should get the focus a name in your XAML, i.e.,
<TextBox x:Name="textBox1"/>
On your view model, add a property for the Action:
public Action FocusAction {get;set;}
Some time before or as your view is loading, get your DataContext (i.e., your view model) and add the Action to the code behind (the view's .cs file):
ViewModel vm = (ViewModel)this.DataContext;
if ( vm.FocusAction == null )
vm.FocusAction= new Action(() => this.textBox1.Focus());
For instance, you might implement the IsLoaded event and do it there. You could also do this in your constructor after InitializeComponent, as long as it either created your view model instance or it was passed in as a paramater. The key is that the view model is already instantiated and assigned to the view's data context.
Then in your view model, wherever you wanted to add focus to that textbox, call:
FocusAction();
Since what I just showed above requires that your view cast the DataContext to a particular view model, what I do is create an interface for the kinds of actions I need, like this:
interface IFocusable
{
Action FocusAction {get;set;}
}
Then I make my view model implement that inteface. With that done, in my view's code-behind, I can say something like:
if(this.DataContext is IFocusable)
((IFocusable)this.DataContext).FocusAction = new Action(() => this.textBox1.Focus());
I think that makes it more MVVM-compliant, since it's not tightly-coupled to a particular view model, the view just knows it can add an action if the view model is the type of view model that can use it.
More details and another example available here: http://jkshay.com/closing-a-wpf-window-using-mvvm-and-minimal-
code-behind/
I know it's been few years but I just faced the same, so here's my answer in case someone will find it useful...
In your xaml file refer to your ViewModel class in the DataContext:
<Window.DataContext>
<local:YourViewModel x:Name="yourViewModel"/>
</Window.DataContext>
In your ViewModel class create a public function with your xaml file as argument and a private member to hold it:
private MainWindow mainWindow;
public void OnViewInitialized(MainWindow mainWindow)
{
this.mainWindow = mainWindow;
}
In your code behind use the function pass the the 'yourViewModel' you defined in the xaml:
public MainWindow()
{
InitializeComponent();
yourViewModel.OnViewInitialized(this);
}
And that's it :)
Now you can access all your xaml elements from your ViewModel by using your mainWindow member, just give them a name.
mainWindow.textBox1
you can create a constructor for view model which accepts the ContentPage as its parameter.
public class ViewModelClass
{
public ViewModelClass(ContentPage p=null)
{...}
}
then set the binding context in Contentpage back code script passing the referenc of contentpage to viewmodel
public class ContentPageClass : ContentPage
{
public ContentPageClass()
{
BindingContext = new ViewModelClass(p:this);
}
}

How to create custom Adorner ContentControl?

I'm trying to create an adorner layer in which I can place, e.g., notifications. I want to make it general and XAML-authored as much as possible, so I made new a ContentControl, called AdornerContentControl, used like this:
<AdornerDecorator Grid.Row="1" Name="RichTextBoxAdornerDecorator">
<loc:AdornerContentControl x:Name="TestAdorner">
<loc:AdornerContentControl.Decorator>
<Button Width="100" Height="100">ZZZ</Button>
</loc:AdornerContentControl.Decorator>
<RichTextBox>
<!-- some more stuff... -->
</RichTextBox>
</loc:AdornerContentControl>
</AdornerDecorator>
This is the code:
public class AdornerContentControl : ContentControl {
private SimpleAdorner _adorner;
public static readonly DependencyProperty DecoratorProperty = DependencyProperty.Register(
"Decorator", typeof (Control), typeof (AdornerContentControl), new PropertyMetadata(default(Control),
OnDecoratorPropertyChanged));
private static void OnDecoratorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { /*...*/ }
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
try { _adorner = new SimpleAdorner((UIElement)Content, Decorator); }
catch (NullReferenceException) { }
}
public Control Decorator
{
get { return (Control) GetValue(DecoratorProperty); }
set { SetValue(DecoratorProperty, value); }
}
}
public class SimpleAdorner : Adorner{
public SimpleAdorner([NotNull] UIElement adornedElement, Control content) : base(adornedElement)
{
_visuals = new VisualCollection(this);
_adornerLayer = AdornerLayer.GetAdornerLayer(adornedElement);
if (content == null) return;
_visuals.Add(_contentPresenter = new ContentPresenter()
{
Content = content
});
_adornerLayer.Add(this);
}
}
But AdornerLayer.GetAdornerLayer(adornedElement) always returns null (the SimpleAdorner is constructed in OnInitialized).
Should I explicitly create AdornerDecorator ?
Am I supposed to call Content.UpdateLayout somewhere ?

Windows 8 XAML Databinding on updating on text changed

I have a windows 8 XAML/C# application using the MVVM pattern.
All my textboxes on the form have their text properties bound to properties on my MVVM class.
So, one of my textboxes looks like this:
<TextBox x:Name="textAddressLine1" Text="{Binding AddressLine1, Mode=TwoWay}"/>
And that property on the MVVM class looks like this:
private string addressLine1;
public string AddressLine1
{
get { return addressLine1; }
set
{
if (addressLine1 == value)
{
return;
}
addressLine1 = value;
RaisePropertyChanged("AddressLine1");
}
}
As I type into my textbox the MVVM class isn't updated. It only gets updated once the focus moves to a different control.
How can I update the MVVM class property when the text changes on my textbox?
Thanks in advance
Use explicit binding for
textAddressLine1
<TextBox x:Name="textAddressLine1" Text="{Binding AddressLine1,UpdateSourceTrigger=Explicit, Mode=TwoWay}" TextChanged="textAddressLine1_Changed"/>
private void textAddressLine1_Changed(object sender, RoutedEventArgs e)
{
BindingExpression be = textAddressLine1.GetBindingExpression(TextBox.TextProperty);
be.UpdateTarget();
}
I didn't test the code but should work.
EDIT: I see it UpdateSourceTrigger is not exist for environtment
You can create a your viewmodel as instance and give it as datacontext by the way you can easily perform your viewmodel from your code-behind. For this type cases it saves the day!
public MyClassViewModel ViewModel {get;set}
ctor()
{
this.ViewModel=new MyClassViewModel();
this.DataContext=this.ViewModel;
InitializeComponets();
}
private void textAddressLine1_Changed(object sender, RoutedEventArgs e)
{
this.ViewModel.AddressLine1=textAddressLine1.Text;
}
I think you will not need two way by this way. OneWay is ok. Because you explicitly change VM.
Note:I coded on SO, didn't test again.Hope helps!
I had the same issue, I found here: https://stackoverflow.com/a/11676076/4551080
<TextBox Text="{Binding Path=EmailAddress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
So make sure to set UpdateSourceTrigger=PropertyChanged Default Value is LostFocus
Use this workaround:
public class ExtendedTextBox : TextBox
{
public static readonly DependencyProperty CustomActionProperty =
DependencyProperty.Register(
"CustomAction",
typeof(Action<string>),
typeof(ExtendedTextBox),
new PropertyMetadata(null, OnPropertyChanged));
public Action<string> CustomAction
{
get
{
return (Action<string>)GetValue(CustomActionProperty);
}
set
{
SetValue(CustomActionProperty, value);
}
}
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if(e.NewValue != null)
(d as ExtendedTextBox).TextChanged += ExtendedTextBox_TextChanged;
else
(d as ExtendedTextBox).TextChanged -= ExtendedTextBox_TextChanged;
}
async static void ExtendedTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
await CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => (sender as ExtendedTextBox).CustomAction((sender as ExtendedTextBox).Text));
}
}
IN your model:
public Action<string> UpdateBindedViewModelProperty
{
get { return new Action<string>((value) => NewLabelName = value); }
}
and view:
<plmrfc:extendedtextbox customaction="{Binding UpdateBindedViewModelProperty, Mode=OneTime}" text="{Binding Path=NewLabelName, Mode=TwoWay}" width="200" x:name="Label_TextBox"></plmrfc:extendedtextbox>
There is also another way, which does not involve subclassing TextBox. Maybe you like this one more:
using System.Reflection;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Flexman
{
public class TextBoxUpdateSourceBehaviour
{
private static PropertyInfo _boundProperty;
public static readonly DependencyProperty BindingSourceProperty =
DependencyProperty.RegisterAttached(
"BindingSource",
typeof(string),
typeof(TextBoxUpdateSourceBehaviour),
new PropertyMetadata(default(string), OnBindingChanged));
public static void SetBindingSource(TextBox element, string value)
{
element.SetValue(BindingSourceProperty, value);
}
public static string GetBindingSource(TextBox element)
{
return (string)element.GetValue(BindingSourceProperty);
}
private static void OnBindingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var txt = d as TextBox;
if (txt == null)
return;
txt.Loaded += OnLoaded;
txt.TextChanged += OnTextChanged;
}
static void OnLoaded(object sender, RoutedEventArgs e)
{
var txt = sender as TextBox;
if (txt == null)
return;
// Reflect the datacontext of the textbox to find the field to bind to.
var dataContextType = txt.DataContext.GetType();
_boundProperty = dataContextType.GetRuntimeProperty(GetBindingSource(txt));
// If you want the behaviour to handle your binding as well, uncomment the following.
//var binding = new Binding();
//binding.Mode = BindingMode.TwoWay;
//binding.Path = new PropertyPath(GetBindingSource(txt));
//binding.Source = txt.DataContext;
//BindingOperations.SetBinding(txt, TextBox.TextProperty, binding);
}
static void OnTextChanged(object sender, TextChangedEventArgs e)
{
var txt = sender as TextBox;
if (txt == null)
return;
if (_boundProperty.GetValue(txt.DataContext).Equals(txt.Text)) return;
_boundProperty.SetValue(txt.DataContext, txt.Text);
}
}
}
and view
<TextBox Text="{Binding Username}" Flexman:TextBoxUpdateSourceBehaviour.BindingSource="Username" />
This is the prettiest solution I know of. Others will be "non-generic" hacks.
Good luck. I do agree that it's major downgrade from silverlight/WPF but hey, there are a lot of more horrible things in WinRT that are missing in WPF :)

Categories

Resources