Wpf disable repeatbuttons when scrolled to top/bottom - wpf

I'm making a touchscreen interface that uses a listbox.
I have a button above and below the listbox for page up/down.
I'm trying to get it to where when scrolled all the way up the pageup button gets disabled.
and when scrolled all the way down the pagedown button gets disabled too.
Here's the code in my Styles.xaml for the Listbox
<Style x:Key="{x:Type ListBox}" TargetType="{x:Type ListBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate x:Key="{x:Type ListBox}" TargetType="{x:Type ListBox}">
<DockPanel>
<RepeatButton x:Name="LineUpButton" DockPanel.Dock="Top"
HorizontalAlignment="Stretch"
Height="50"
Content="/\"
Command="{x:Static ScrollBar.PageUpCommand}"
CommandTarget="{Binding ElementName=scrollviewer}" />
<RepeatButton x:Name="LineDownButton" DockPanel.Dock="Bottom"
HorizontalAlignment="Stretch"
Height="50"
Content="\/"
Command="{x:Static ScrollBar.PageDownCommand}"
CommandTarget="{Binding ElementName=scrollviewer}" />
<Border BorderThickness="1" BorderBrush="Gray" Background="White">
<ScrollViewer x:Name="scrollviewer">
<ItemsPresenter/>
</ScrollViewer>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
And here's where I instantiate the listbox
<ListBox SelectedItem="{Binding SelectedCan}" ItemsSource="{Binding Path=SelectedKioskCashCans}">
<ListBox.ItemTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding image}" MaxWidth="75" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
I searched all around yesterday with no luck.
I'm hoping to be able to do it all in xaml.
I'm using images for the buttons but took them out for readability above,
they really look like...
<RepeatButton x:Name="LineUpButton" DockPanel.Dock="Top" HorizontalAlignment="Stretch"
Height="50"
Command="{x:Static ScrollBar.PageUpCommand}"
CommandTarget="{Binding ElementName=scrollviewer}">
<RepeatButton.Template>
<ControlTemplate TargetType="{x:Type RepeatButton}">
<Grid>
<Image Name="Normal" Source="/Images/up.png"/>
<Image Name="Pressed" Source="/Images/up.png" Visibility="Hidden"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</RepeatButton.Template>
</RepeatButton>

Just use CanExecute method of the PageUpCommand for that. Return false if where are no pages left and the button will become disabled automatically.
EDIT:
I have created a simple attached behavior that can be used to fix this problem. Just set the following attached property on the ScrollViewer:
<ScrollViewer x:Name="scrollviewer"
z:ScrollBarCommandsCanExecuteFixBehavior.IsEnabled="True">
<ItemsPresenter/>
</ScrollViewer>
And here is the source code of the behavior:
public static class ScrollBarCommandsCanExecuteFixBehavior
{
#region Nested Types
public class CommandCanExecuteMonitor<T> where T : UIElement
{
protected T Target { get; private set; }
protected CommandCanExecuteMonitor(T target, RoutedCommand command)
{
Target = target;
var binding = new CommandBinding(command);
binding.CanExecute += OnCanExecute;
target.CommandBindings.Add(binding);
}
protected virtual void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
}
}
public class PageUpCanExecuteMonitor : CommandCanExecuteMonitor<ScrollViewer>
{
public PageUpCanExecuteMonitor(ScrollViewer scrollViewer)
: base(scrollViewer, ScrollBar.PageUpCommand)
{
}
protected override void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (e.Handled)
{
return;
}
if (Equals(Target.VerticalOffset, 0.0))
{
e.CanExecute = false;
e.Handled = true;
}
}
}
public class PageDownCanExecuteMonitor : CommandCanExecuteMonitor<ScrollViewer>
{
public PageDownCanExecuteMonitor(ScrollViewer scrollViewer)
: base(scrollViewer, ScrollBar.PageDownCommand)
{
}
protected override void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (e.Handled)
{
return;
}
if (Equals(Target.VerticalOffset, Target.ScrollableHeight))
{
e.CanExecute = false;
e.Handled = true;
}
}
}
#endregion
#region IsEnabled Attached Property
public static bool GetIsEnabled(DependencyObject obj)
{
return (bool) obj.GetValue(IsEnabledProperty);
}
public static void SetIsEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsEnabledProperty, value);
}
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached("IsEnabled", typeof (bool), typeof (ScrollBarCommandsCanExecuteFixBehavior), new PropertyMetadata(false, OnIsEnabledChanged));
private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((bool) e.NewValue)
{
var scrollViewer = d as ScrollViewer;
if (scrollViewer != null)
{
OnAttached(scrollViewer);
}
else
{
throw new NotSupportedException("This behavior only supports ScrollViewer instances.");
}
}
}
private static void OnAttached(ScrollViewer target)
{
SetPageUpCanExecuteMonitor(target, new PageUpCanExecuteMonitor(target));
SetPageDownCanExecuteMonitor(target, new PageDownCanExecuteMonitor(target));
}
#endregion
#region PageUpCanExecuteMonitor Attached Property
private static void SetPageUpCanExecuteMonitor(DependencyObject obj, PageUpCanExecuteMonitor value)
{
obj.SetValue(PageUpCanExecuteMonitorProperty, value);
}
private static readonly DependencyProperty PageUpCanExecuteMonitorProperty =
DependencyProperty.RegisterAttached("PageUpCanExecuteMonitor", typeof (PageUpCanExecuteMonitor), typeof (ScrollBarCommandsCanExecuteFixBehavior), new PropertyMetadata(null));
#endregion
#region PageDownCanExecuteMonitor Attached Property
private static void SetPageDownCanExecuteMonitor(DependencyObject obj, PageDownCanExecuteMonitor value)
{
obj.SetValue(PageDownCanExecuteMonitorProperty, value);
}
private static readonly DependencyProperty PageDownCanExecuteMonitorProperty =
DependencyProperty.RegisterAttached("PageDownCanExecuteMonitor", typeof (PageDownCanExecuteMonitor), typeof (ScrollBarCommandsCanExecuteFixBehavior), new PropertyMetadata(null));
#endregion
}
The basic idea is that we add a CommandBinding to the ScrollViewer for each of the commands and subscribe to the CanExecute event on those bindings. In the event handler we check the current position of the scroll and set the e.CanExecute property accordingly.

Related

Change SliderThumbStyle value from Slider definition

I have this SliderThumbStyle:
<Style x:Key="SliderThumbStyle" TargetType="{x:Type Thumb}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Grid>
<Border Name="outerBorder"
Background="{DynamicResource ApplicationBorderBrush}"
BorderBrush="{DynamicResource ApplicationBorderBrush}"
Height="24"
Width="24"
Opacity="1"
BorderThickness="2"
CornerRadius="10"/>
<TextBlock x:Name="sliderValue"
FontSize="10"
Foreground="Silver"
Text="{Binding Value, RelativeSource={RelativeSource AncestorType=Slider}, StringFormat={}{0:N1}}"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
In my application i am using this Slider Style twice but one of them not needed this N1 StringFormat but N0for only integer values.
Any idea how to choose this when i define my Slider in advanced ?
As mm8 suggestive i try this:
<Slider Tag="{Binding Value, RelativeSource={RelativeSource Self}, StringFormat=N1}" />
Style:
Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=Slider}}"/>
But it seems that it now show the value in N1 format but 1.23456789
I also try this:
Tag="{Binding Value, RelativeSource={RelativeSource Self}, StringFormat={}{0:N1}}"
I am afraid you can't change the StringFormat without modifying the Style. What you could do is to bind to the Tag property of the Slider in your Style:
<TextBlock x:Name="sliderValue"
FontSize="10"
Foreground="Silver"
Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=Slider}}"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
...and then handle the ValueChanged event of each individual Slider and set the Tag property to a formatted string:
private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Slider slider = (Slider)sender;
slider.Tag = slider.Value.ToString("N1");
}
You may want to wrap this functionality an attached behaviour:
public class SliderFormatBehavior
{
public static string GetStringFormat(Slider treeViewItem)
{
return (string)treeViewItem.GetValue(StringFormatProperty);
}
public static void SetStringFormat(Slider slider, string value)
{
slider.SetValue(StringFormatProperty, value);
}
public static readonly DependencyProperty StringFormatProperty =
DependencyProperty.RegisterAttached(
"StringFormat",
typeof(string),
typeof(SliderFormatBehavior),
new UIPropertyMetadata(null, OnStringFormatChanged));
static void OnStringFormatChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
Slider slider = depObj as Slider;
if (slider != null)
{
if (slider.IsLoaded)
{
SetTag(slider);
}
else
{
slider.Loaded += Slider_Loaded;
}
slider.ValueChanged += Slider_ValueChanged;
}
}
private static void Slider_Loaded(object sender, RoutedEventArgs e)
{
Slider slider = (Slider)sender;
SetTag(slider);
slider.Loaded -= Slider_Loaded;
}
private static void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
SetTag((Slider)sender);
}
private static void SetTag(Slider slider)
{
slider.Tag = slider.Value.ToString(GetStringFormat(slider));
}
}
Sample usage:
<Slider ... local:SliderFormatBehavior.StringFormat="N1" />

How To Search In ComboBox With TextBox?

I Have a ComboBox With 3 Items (Room,Class,HighSchool) And a TextBox.
I Want When i Write Room IN TextBox Immediately Combobox.
SelectedItem = Room
You'd be better off using a custom control. I use an intellisense text box that you bind to a list, when you start typing it will automatically select any item in the list with that character.
window xaml below;
<UserControl.Resources>
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="_Border"
Padding="2"
SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="_Border" Property="Background" Value="Gray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<TextBox Name="textbox" Grid.Column="1" Grid.Row="2" GotMouseCapture="textbox_GotMouseCapture"
GotFocus="textbox_GotFocus" PreviewKeyDown="textbox_PreviewKeyDown" TextChanged="textbox_TextChanged"
DataContext="{Binding ElementName=intelliWin}"
Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="{Binding ElementName=intelliWin, Path=Width}" TextAlignment="Center" Height="20"/>
<Popup Name="popup" Height="Auto" Width="Auto" MinWidth="180" StaysOpen="False" Placement="Bottom"
PlacementTarget="{Binding ElementName=textbox}" HorizontalAlignment="Left">
<Popup.Style>
<Style TargetType="Popup">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="IsOpen" Value="False"/>
</Trigger>
</Style.Triggers>
</Style>
</Popup.Style>
<Grid>
<ListBox Name="listbox" ScrollViewer.HorizontalScrollBarVisibility="Hidden"
MouseUp="listbox_MouseUp" ItemContainerStyle="{StaticResource ListBoxItemStyle}">
</ListBox>
</Grid>
</Popup>
</Grid>
to use it in the window you need to do this
<local:IntellisenseStyleTextbox Grid.Column="1" Grid.Row="6" Text="{Binding YOUR LIST DATA, UpdateSourceTrigger=PropertyChanged}" x:Name="intelliseText" Width="200" HorizontalContentAlignment="Center"/>'
then add this to the .cs file
public partial class IntellisenseStyleTextbox : UserControl, INotifyPropertyChanged
{
public IntellisenseStyleTextbox()
{
InitializeComponent();
(this.Content as FrameworkElement).DataContext = this;
}
public List<string> PossibleItems { get; set; }
public event EventHandler ItemSelected;
protected void OnItemSelected(EventArgs e)
{
EventHandler handler = ItemSelected;
if (handler != null)
{
handler(this, e);
}
}
// Bindable Text property from http://blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html
public string Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValueDp(TextProperty, value);
}
}
// Required to allow items to bind to Text property
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string),
typeof(IntellisenseStyleTextbox), null);
public event PropertyChangedEventHandler PropertyChanged;
void SetValueDp(DependencyProperty property, object value,
[System.Runtime.CompilerServices.CallerMemberName] string p = null)
{
SetValue(property, value);
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
public List<string> GetDisplayedModelNames()
{
// Only display items that have the current text at the start of them
List<string> toDisplay = new List<string>();
string match = textbox.Text.ToLower();
foreach (string possibleItem in PossibleItems)
{
string lower = possibleItem.ToLower();
if (lower.Contains(match))
{
toDisplay.Add(possibleItem);
}
}
return toDisplay;
}
private void textbox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
switch (e.Key)
{
// Up and down arrow keys move up and down list box
case Key.Down:
listbox.SelectedIndex++;
listbox.ScrollIntoView(listbox.SelectedItem);
break;
case Key.Up:
// Make sure item is always selected
if (listbox.SelectedIndex > 0)
{
listbox.SelectedIndex--;
listbox.ScrollIntoView(listbox.SelectedItem);
}
break;
// Enter key fills in OutputName with selected text
case Key.Enter:
SelectListBoxItem();
break;
case Key.Back:
popup.IsOpen = true;
break;
}
base.OnPreviewKeyDown(e);
}
private void SelectListBoxItem()
{
if (listbox.SelectedIndex < 0) return;
List<string> outputNames = GetDisplayedModelNames();
if (listbox.SelectedIndex < outputNames.Count)
{
this.Text = outputNames[listbox.SelectedIndex];
// Move caret to end of text
textbox.CaretIndex = this.Text.Count();
popup.IsOpen = false;
OnItemSelected(new EventArgs());
}
}
private void listbox_MouseUp(object sender, MouseButtonEventArgs e)
{
SelectListBoxItem();
}
private void textbox_GotMouseCapture(object sender, MouseEventArgs e)
{
OpenPopup();
}
private void textbox_GotFocus(object sender, RoutedEventArgs e)
{
OpenPopup();
}
private void OpenPopup()
{
listbox.ItemsSource = GetDisplayedModelNames();
popup.IsOpen = true;
}
private void textbox_TextChanged(object sender, TextChangedEventArgs e)
{
OnPropertyChanged(new DependencyPropertyChangedEventArgs(TextProperty, textbox.Text, textbox.Text));
// Update listbox with relevant names
if (PossibleItems == null) return;
listbox.ItemsSource = this.GetDisplayedModelNames();
}
}

Issues with MouseDoubleClick command on TreeViewItem using AttachedProperties

I am trying to attach a Command and CommandParameter property to a TreeViewItem so I can route a mouse double click event to a Command in the ViewModel. It appears that the double click event is never even fired.
MouseDoubleClick:
namespace VisualInspectionConsole.Commands
{
public class MouseDoubleClick
{
public static DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached(
"Command",
typeof(ICommand),
typeof(MouseDoubleClick),
new UIPropertyMetadata(CommandChanged));
public static DependencyProperty CommandParameterProperty =
DependencyProperty.RegisterAttached(
"CommandParameter",
typeof(object),
typeof(MouseDoubleClick),
new UIPropertyMetadata(null));
public static void SetCommand(DependencyObject target, ICommand value)
{
target.SetValue(CommandProperty, value);
}
public static void SetCommandParameter(DependencyObject target, object value)
{
target.SetValue(CommandParameterProperty, value);
}
public static object GetCommandParameter(DependencyObject target)
{
return target.GetValue(CommandParameterProperty);
}
private static void CommandChanged(
DependencyObject target, DependencyPropertyChangedEventArgs e)
{
Control control = target as Control;
if (control != null)
{
if ((e.NewValue != null) && (e.OldValue == null))
{
control.MouseDoubleClick += OnMouseDoubleClick;
}
else if ((e.NewValue == null) && (e.OldValue != null))
{
control.MouseDoubleClick -= OnMouseDoubleClick;
}
}
}
private static void OnMouseDoubleClick(object sender, RoutedEventArgs e)
{
Control control = sender as Control;
((ICommand) control.GetValue(CommandProperty)).Execute(
control.GetValue(CommandParameterProperty));
}
}
}
Tree view code in MainWindow.xaml:
<Window
...................
xmlns:commands ="clr-namespace:VisualInspectionConsole.Commands"
...................
<TreeView
Width="275"
Name="tvwWaferList"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ItemsSource="{Binding TreeviewViewModel.WaferList}"
SelectedValuePath="Wafer"
Background="{StaticResource accentBrushOne}"
Foreground="Black"
FontFamily="SegoeUI"
FontWeight="Bold"
Margin="1"
BorderBrush="{StaticResource primaryBrush}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Foreground" Value="{StaticResource primaryBrush}"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter
Property="commands:MouseDoubleClick.Command"
Value="{Binding SelectWaferCommand}"/>
<Setter
Property="commands:MouseDoubleClick.CommandParameter"
Value="{Binding}"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Wafers}">
<TextBlock
Text="{Binding}"
Foreground="Black"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Can anyone tell me why the mouse double click is not working?
Edit: Getting this error in the output window:
System.Windows.Data Error: 40 : BindingExpression path error: 'SelectWaferCommand' property not found on 'object' ''WaferHierarchyModel' (HashCode=4494425)'. BindingExpression:Path=SelectWaferCommand; DataItem='WaferHierarchyModel' (HashCode=4494425); target element is 'TreeViewItem' (Name=''); target property is 'Command' (type 'ICommand')
So after some reading I was able to get this work correctly. I just had to change the commands:MouseDoubleClick.Commands value
from
{Binding SelectWaferCommand}"
to
{Binding DataContext.TreeviewViewModel.SelectWaferCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeView}}}"
because the WaferHierarchyModel objects do not have a SelectWaferCommand, the TreeviewViewModel does. This basically tells it to execute the SelectWaferCommand found in the nearest TreeView ancestors DataContext.TreeviewViewModel object.

Databinding to Command in Silverlight Templated Button control

I am trying to create a templated button control with databinding for the Visibility, tooltip, and Command. The Visibility binding works, as does the tooltip, but the Command does not. Another process is responsible for injecting the viewmodel and associating it with the View, and the other data bindings are working so I am pretty confident that is working properly.
In the resource dictionary:
<Converters:BoolToVisibilityConverter x:Key="boolVisibilityConverter" />
<Style TargetType="local:ImageButton">
<Setter Property="Visibility" Value="{Binding FallbackValue=Visible, Path=ToolIsAvailable, Converter={StaticResource boolVisibilityConverter} }"/>
<Setter Property="Command" Value="{Binding ButtonCommand}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ImageButton">
<Grid>
<Image Source="{TemplateBinding Image}"
ToolTipService.ToolTip="{Binding ToolName}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
the templated control
public class MyButton: ImageButton
{
public MyButton(MyCommandViewModel viewmodel)
{
this.DefaultStyleKey = typeof(ImageButton);
this.Image = new BitmapImage(new Uri("/MyProject;component/Themes/myimage.png", UriKind.Relative));
this.DataContext = viewmodel;
}
}
and in the view model
public MyCommandViewModel()
: base("My Tool", true)
{
}
public class CommandViewModel
{
public CommandViewModel(string toolName, bool isAvailable)
{
ToolIsAvailable = isAvailable;
ToolName = toolName;
_buttoncommand = new DelegateCommand(() =>
{
ExecuteCommand();
},
() => { return CanExecute; });
}
private bool _canExecute = true;
public bool CanExecute
{
get { return _canExecute; }
set
{
_canExecute = value;
OnPropertyChanged("CanExecute");
if (_command != null) _command.RaiseCanExecuteChanged();
}
}
private DelegateCommand _buttoncommand;
public ICommand ButtonCommand
{
get { return _buttoncommand; }
}
protected virtual void ExecuteCommand()
{
}
public bool ToolIsAvailable
{
get { return _toolIsReady; }
set { _toolIsReady = value; OnPropertyChanged("ToolIsAvailable"); }
}
public string ToolName
{
get { return _toolName; }
set { _toolName = value; OnPropertyChanged("ToolName"); }
}
}
Why are the other databindings functioning properly but not the Command data binding. I found this similar post
Overriding a templated Button's Command in WPF
Do I need to template a grid control instead and use RoutedCommands? I am not sure I understand why Silverlight treats the Command binding different than the others so I suspect I just have a bug in the code.
Does specifically looking for the datacontext work?
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ButtonCommand}"
This was my solution. Using the same commandviewmodel as above and same MyCommandViewModel
<Style TargetType="local:ImageButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ImageButton">
<Grid>
<Image Source="{TemplateBinding Image}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The databinding is now done in a user control
<UserControl x:Class="SilverlightApplication11.Test"
...
>
<UserControl.Resources>
<Converters:BoolToVisibilityConverter x:Key="boolVisibilityConverter" />
</UserControl.Resources>
<Grid>
<local:ImageButton Image="/SilverlightApplication11;component/Themes/hand.png" Command="{Binding ButtonCommand}" Visibility="{Binding FallbackValue=Visible, Path=ToolIsAvailable, Converter={StaticResource boolVisibilityConverter} }"/>
</Grid>
</UserControl>
and the code behind
public Test(TestCommandViewModel vm)
{
InitializeComponent();
this.Loaded += (o, e) => this.DataContext = vm;
}

WPF Watermark PasswordBox from Watermark TextBox

I am using a Watermark textbox as in Watermark TextBox in WPF
<Grid Grid.Row="0" Background="{StaticResource brushWatermarkBackground}" Style="{StaticResource EntryFieldStyle}" >
<TextBlock Margin="5,2" Text="This prompt dissappears as you type..." Foreground="{StaticResource brushWatermarkForeground}"
Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBox Name="txtUserEntry" Background="Transparent" BorderBrush="{StaticResource brushWatermarkBorder}" />
</Grid>
How can I apply this for a PasswordBox?
The general approach is the same: write a custom control style, and show the watermark whenever the password box is empty. The only problem here is that PasswordBox.Password is not a dependency property, and you can't use it in a trigger. Also PasswordBox is sealed, so you can't override this notification behavior, but you can use attached properties here.
The following code demonstrates how.
XAML:
<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfTest="clr-namespace:WpfTest"
Title="Password Box Sample" Height="300" Width="300">
<Window.Resources>
<Style x:Key="{x:Type PasswordBox}"
TargetType="{x:Type PasswordBox}">
<Setter Property="WpfTest:PasswordBoxMonitor.IsMonitoring"
Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type PasswordBox}">
<Border Name="Bd"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
SnapsToDevicePixels="true">
<Grid>
<ScrollViewer x:Name="PART_ContentHost"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<TextBlock Text="Please enter your password"
Margin="4, 2, 0, 0"
Foreground="Gray"
Visibility="Collapsed"
Name="txtPrompt" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled"
Value="false">
<Setter TargetName="Bd"
Property="Background"
Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
<Trigger Property="WpfTest:PasswordBoxMonitor.PasswordLength" Value="0">
<Setter Property="Visibility" TargetName="txtPrompt" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<PasswordBox VerticalAlignment="Top"/>
</Grid>
</Window>
C#:
using System.Windows;
using System.Windows.Controls;
namespace WpfTest {
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
}
}
public class PasswordBoxMonitor : DependencyObject {
public static bool GetIsMonitoring(DependencyObject obj) {
return (bool)obj.GetValue(IsMonitoringProperty);
}
public static void SetIsMonitoring(DependencyObject obj, bool value) {
obj.SetValue(IsMonitoringProperty, value);
}
public static readonly DependencyProperty IsMonitoringProperty =
DependencyProperty.RegisterAttached("IsMonitoring", typeof(bool), typeof(PasswordBoxMonitor), new UIPropertyMetadata(false, OnIsMonitoringChanged));
public static int GetPasswordLength(DependencyObject obj) {
return (int)obj.GetValue(PasswordLengthProperty);
}
public static void SetPasswordLength(DependencyObject obj, int value) {
obj.SetValue(PasswordLengthProperty, value);
}
public static readonly DependencyProperty PasswordLengthProperty =
DependencyProperty.RegisterAttached("PasswordLength", typeof(int), typeof(PasswordBoxMonitor), new UIPropertyMetadata(0));
private static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var pb = d as PasswordBox;
if (pb == null) {
return;
}
if ((bool) e.NewValue) {
pb.PasswordChanged += PasswordChanged;
} else {
pb.PasswordChanged -= PasswordChanged;
}
}
static void PasswordChanged(object sender, RoutedEventArgs e) {
var pb = sender as PasswordBox;
if (pb == null) {
return;
}
SetPasswordLength(pb, pb.Password.Length);
}
}
}
Please notice PasswordBoxMonitor in XAML code.
you can show/hide the background by yourself instead of using triggers:
XAML:
<PasswordBox x:Name="passwordBox" PasswordChanged="passwordChanged"
Background="{StaticResource PasswordHint}" />
Code behind:
// helper to hide watermark hint in password field
private void passwordChanged(object sender, RoutedEventArgs e)
{
if (passwordBox.Password.Length == 0)
passwordBox.Background.Opacity = 1;
else
passwordBox.Background.Opacity = 0;
}
you can use my approach for a watermark behavior. all you have to do is copy and paste the TextBoxWatermarkBehavior and the change the Behavior<TextBox> to Behavior<PasswordBox>.
you can find a demo project here
#blindmeis's suggestion is good. For PasswordBox the class would be as follows.
public class PasswordBoxWatermarkBehavior : System.Windows.Interactivity.Behavior<PasswordBox>
{
private TextBlockAdorner adorner;
private WeakPropertyChangeNotifier notifier;
#region DependencyProperty's
public static readonly DependencyProperty LabelProperty =
DependencyProperty.RegisterAttached("Label", typeof(string), typeof(PasswordBoxWatermarkBehavior));
public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}
public static readonly DependencyProperty LabelStyleProperty =
DependencyProperty.RegisterAttached("LabelStyle", typeof(Style), typeof(PasswordBoxWatermarkBehavior));
public Style LabelStyle
{
get { return (Style)GetValue(LabelStyleProperty); }
set { SetValue(LabelStyleProperty, value); }
}
#endregion
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.Loaded += this.AssociatedObjectLoaded;
this.AssociatedObject.PasswordChanged += AssociatedObjectPasswordChanged;
}
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.Loaded -= this.AssociatedObjectLoaded;
this.AssociatedObject.PasswordChanged -= this.AssociatedObjectPasswordChanged;
this.notifier = null;
}
private void AssociatedObjectPasswordChanged(object sender, RoutedEventArgs e)
{
this.UpdateAdorner();
}
private void AssociatedObjectLoaded(object sender, System.Windows.RoutedEventArgs e)
{
this.adorner = new TextBlockAdorner(this.AssociatedObject, this.Label, this.LabelStyle);
this.UpdateAdorner();
//AddValueChanged for IsFocused in a weak manner
this.notifier = new WeakPropertyChangeNotifier(this.AssociatedObject, UIElement.IsFocusedProperty);
this.notifier.ValueChanged += new EventHandler(this.UpdateAdorner);
}
private void UpdateAdorner(object sender, EventArgs e)
{
this.UpdateAdorner();
}
private void UpdateAdorner()
{
if (!String.IsNullOrEmpty(this.AssociatedObject.Password) || this.AssociatedObject.IsFocused)
{
// Hide the Watermark Label if the adorner layer is visible
this.AssociatedObject.TryRemoveAdorners<TextBlockAdorner>();
}
else
{
// Show the Watermark Label if the adorner layer is visible
this.AssociatedObject.TryAddAdorner<TextBlockAdorner>(adorner);
}
}
}

Resources