I am using two TextBox and "Save" Button. Basically, the "Save" button will be enabled when TextBox has any Text changed. I created a CommandBinding in the Window.Resource and "Save" Button uses Command="Save" and Two TextBox use StaticResources for Command binding.
However, the Button is not enabled when I change the text. Use Debug, I can see that my flag for the TextBox text changed is True but it looks like TextBox didn't trigger the Save Command CanExecuted Event.
Below is my code.
xaml
<Window>
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.New" Executed="NewCommand_Executed" />
<CommandBinding Command="{x:Static commands:DataCommands.Requery}" Executed="RequeryCommand_Executed"/>
<CommandBinding Command="{x:Static commands:DataCommands.ApplicationUndo}"
Executed="ApplicationUndo_OnExecuted" CanExecute="ApplicationUndo_OnCanExecute"/>
</Window.CommandBindings>
<Window.Resources>
<CommandBinding x:Key="Binding" Command="ApplicationCommands.Save"
Executed="SaveCommand_Executed" CanExecute="SaveCommand_CanExecute"/>
</Window.Resources>
<StackPanel>
<Menu>
<MenuItem Header="File">
<MenuItem Command="New"/>
</MenuItem>
</Menu>
<StackPanel Orientation="Horizontal" Margin="5">
<Button Name="New" Command="New" Content="New" Margin="3" Padding="3"/>
<Button Name="Save" Command="Save" Content="Save" Margin="3" Padding="3"/>
...
</StackPanel>
<TextBox Name="TbInputText1" TextChanged="TbInputText_OnTextChanged" Margin="5">
<TextBox.CommandBindings>
<StaticResource ResourceKey="Binding"/>
</TextBox.CommandBindings>
</TextBox>
<TextBox Name="TbInputText2" Margin="5" TextChanged="TbInputText_OnTextChanged">
<TextBox.CommandBindings>
<StaticResource ResourceKey="Binding"/>
</TextBox.CommandBindings>
</TextBox>
<ListBox Name="LsbHistory" DisplayMemberPath="Name" Margin="3"></ListBox>
</StackPanel>
behind-code
public partial class UseCommand : Window
{
private Dictionary<Object, bool> _isDirty = new Dictionary<Object, bool>();
public UseCommand()
{
InitializeComponent();
this.AddHandler(CommandManager.PreviewExecutedEvent,
new ExecutedRoutedEventHandler(CommandExecuted));
}
private void TbInputText_OnTextChanged(object sender, TextChangedEventArgs e)
{
// _isDirty.Add(sender, true);
_isDirty[sender] = true;
}
#region Save
private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (_isDirty.ContainsKey(sender) && _isDirty[sender])
{
e.CanExecute = true;
}
else
{
// MessageBox.Show(sender.ToString());
e.CanExecute = false;
}
}
private void SaveCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
string text = ((TextBox)sender).Text;
MessageBox.Show("About this controller: " + sender.ToString() +
"Contents: " + text);
_isDirty[sender] = false;
}
#endregion
}
Is there any step I missed? Why CanExecuted didn't be triggerd?
You have to move your save CommandBinding to <Window.CommandBindings> collection from the <Window.Resource> collection
remove the x:Key attribute
Set the Command property of your button to ""ApplicationCommands.Save"
<Window.CommandBindings>
<CommandBinding
CanExecute="SaveCommand_CanExecute"
Command="ApplicationCommands.Save"
Executed="SaveCommand_Executed"/>
(...)
</Window.CommandBindings>
<StackPanel>
(...)
<Button
Name="Save"
Margin="3"
Padding="3"
Command="ApplicationCommands.Save"
Content="Save"
/>
(...)
Related
I would like to have a popup show at the bottom of each textbox in my window, as they are focused.
The user would be presented with the last few entries entered in that textbox. I would like the placement to be such that it would be at the bottom of the textbox currently focused.
This is my user control with the textbox:
<UserControl x:Class="PopupPlacement.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<TextBox Name="TextBox_MyControl" Text="enter your text here" Height="25" Width="200"/>
</StackPanel>
</UserControl>
Here is my window:
<Window x:Class="PopupPlacement.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PopupPlacement"
Title="MainWindow" Height="450" Width="800">
<Canvas>
<Grid ShowGridLines="False">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="Domain" Margin="10"/>
<local:MyControl Grid.Column="1" x:Name="Domain" Margin="10"/>
<Label Grid.Row="1" Content="Username" Margin="10"/>
<local:MyControl Grid.Row="1" Grid.Column="1" x:Name="Username" Margin="10"/>
<Label Grid.Row="2" Content="Password" Margin="10"/>
<local:MyControl Grid.Row="2" Grid.Column="1" x:Name="Password" Margin="10"/>
<Button Grid.Row="3" Content="OK" Margin="10" Name="Button_OK"/>
<Button Grid.Row="3" Grid.Column="1" Content="Cancel" Margin="10"/>
<Popup PlacementTarget="{Binding ElementName=TextBox_MyControl}" Placement="Bottom"
IsOpen="{Binding ElementName=TextBox_MyControl, Path=IsKeyboardFocused}">
<ComboBox IsDropDownOpen="True">
<ComboBoxItem IsSelected="True">Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
</ComboBox>
</Popup>
</Grid>
</Canvas>
</Window>
Appreciate any pointers.
For me, the best solution to a similar requirement was to write a Behavior that kind of mimics Intellisense.
I don't have any simple code at hand, but you could create and show a ListBox inside a Popup placed at the AssociatedObject's bottom. You can then bind the TextBox-related entries to the Behavior via a DependencyProperty.
Of course, there's a lot more to it like closing the Popup, re-using existing controls, handling key presses to access the ListBox, insert the selected value to the TextBox etc.
Here's a simple (untested) sketch.
public class IntellisenseBehavior : Behavior<TextBox>
{
public IEnumerable ItemsSource
{
get => (IEnumerable)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(IntellisenseBehavior), new UIPropertyMetadata(null));
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.GotKeyboardFocus += AssociatedObjectOnGotKeyboardFocus;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.GotKeyboardFocus -= AssociatedObjectOnGotKeyboardFocus;
//cleanup
}
private void AssociatedObjectOnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
var popup = new Popup
{
ClipToBounds = false,
AllowsTransparency = true,
PopupAnimation = PopupAnimation.Fade,
HorizontalAlignment = HorizontalAlignment.Left
};
popup.SetValue(FocusManager.IsFocusScopeProperty, true);
popup.Placement = PlacementMode.Bottom;
popup.PlacementTarget = AssociatedObject;
var shadow = new SystemDropShadowChrome { Color = Colors.Transparent, MaxHeight = 200, Margin = new Thickness(0, 0, 5, 5) };
var listBox = new ListBox
{
ItemsSource = ItemsSource
}
((IAddChild)shadow).AddChild(listBox);
((IAddChild)popup).AddChild(shadow);
popup.IsOpen = true;
}
}
Attach it to all TextBoxes that you require to have this functionality and for instance use a converter to get the filtered entries you need.
<!-- Uses converter's public const string NameBox = "NameBox"; for filtering. -->
<TextBox>
<i:Interaction.Behaviors>
<IntellisenseBehavior ItemsSource="{Binding LastEntries, Converter={StaticResource FilterEntriesConverter}, ConverterParameter={x:Static FilterEntriesConverter.NameBox}}" />
</i:Interaction.Behaviors>
</TextBox>
Hope that helps.
I am implementing a Download UI in WPF, where every file that is being downloaded will be shown inside a list box in a DataTemplate
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="FileName" text={Binding FileName}" />
<ProgressBar ... />
<Button Content="Cancel" click="ButtonCancel_Click" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox>
Now this List is getting populated with all the download information perfectly. Only problem I am having is that when user clicks on Cancel button, to cancel the download, I have to remove an entry from the ObservableCollections. But I don't have the File Name in the click event( I know click event is not MVVM, still I want to do it in click event handler).
Can anyone suggest how do I get the FileName of that particular file when the selectedItem gets cancelled. in The
private void ButtonCancel_Click(...) {}
Although I would still encourage you to use MVVM way of dealing with UI events, here's how you can achieve what you want, using Cancel button's click event handler.
First in your xaml, bind file name to Cancel button's Tag property.
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="FileName" text={Binding FileName}" />
<ProgressBar ... />
<Button Content="Cancel" Tag="{Binding FileName}"
Click="ButtonCancel_Click" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox>
Then in your click event handler
private void ButtonCancel_Click(object sender, RoutedEventArgs e)
{
Button myButton = (Button)sender;
string fileName = myButton.Tag.ToString();
// use fileName
}
Edit
Just to add a complete example, that was tested locally, and ensured that works.
XAML
<Window x:Class="WpfTestApp.MainWindow"
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>
<ListBox Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="FileName" Text="{Binding Path=FileName}" />
<Button Content="Cancel" Tag="{Binding Path=FileName}"
Click="ButtonCancel_Click" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Code-behind
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var fileNames = new List<DownloadModel>
{
new DownloadModel
{
FileName = "File1"
},
new DownloadModel
{
FileName = "File2"
},
new DownloadModel
{
FileName = "File3"
}
};
listBox1.ItemsSource = fileNames;
}
private void ButtonCancel_Click(object sender, RoutedEventArgs e)
{
var myButton = sender as Button;
if (myButton.Tag == null)
{
MessageBox.Show("Tag value was null.");
}
else
{
MessageBox.Show(string.Format("File name is {0}", myButton.Tag));
}
}
}
public class DownloadModel
{
public string FileName { get; set; }
}
I have a little WPF Window with 2 TextBoxes Having Ordered TabIndex 0,1 and i want to move focus automatically from the first TextBox to the second when i press Enter Key.
I Use MVVM Light.
Remark : This post is not duplicated. here I do not use Classic approach with event Handler but MVVM Pattern and as you know Code Behind is not allowed in view.
I Found a solution but I don't know if it respect MVVM Principle.
Here the code :
The View
<Window x:Class="WpfMMVLight2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cmd ="http://www.galasoft.ch/mvvmlight"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding MainViewModel}">
<Grid FocusManager.FocusedElement="{Binding ElementName=tb1}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="70"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Text="Text 1" VerticalAlignment="Center"/>
<TextBox x:Name="tb1" Grid.Column="1" VerticalAlignment="Center" Margin="5">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand PassEventArgsToCommand="True"
Command ="{Binding KeyDownCommand, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<TextBlock Grid.Column="0" Grid.Row="1" Text="Text 2" VerticalAlignment="Center"/>
<TextBox x:Name="tb2" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="5">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand PassEventArgsToCommand="True"
Command ="{Binding KeyDownCommand, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
</Grid>
The ViewModel :
private ICommand _keydownCommand;
public ICommand KeyDownCommand
{
get
{
if (_keydownCommand== null)
_keydownCommand= new DelegateCommand<KeyEventArgs>(KeyDownCommandExecute);
return _keydownCommand;
}
}
private void KeyDownCommandExecute(KeyEventArgs e)
{
if (e != null && e.Key == Key.Enter)
{
TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
request.Wrapped = true;
((Control)e.Source).MoveFocus(request);
}
}
}
I don't know if use of "Control" Class in ViewModel Is allowed or not
As you are using MVVM, you can use a Behavior for this:
public class TabOnEnterBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
AssociatedObject.PreviewKeyDown += AssociatedObject_PreviewKeyDown;
}
private void AssociatedObject_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var request = new TraversalRequest(FocusNavigationDirection.Next);
request.Wrapped = true;
AssociatedObject.MoveFocus(request);
}
}
protected override void OnDetaching()
{
AssociatedObject.PreviewKeyDown -= AssociatedObject_PreviewKeyDown;
}
}
In your xaml:
<TextBox>
<i:Interaction.Behaviors>
<wpfTest:TabOnEnterBehavior />
</i:Interaction.Behaviors>
</TextBox>
Add a PreviewKeyDown event to your TextBox first:
<TextBox PreviewKeyDown="OnTextBoxKeyDown" />
Then create a TraversalRequest to move focus to the next item:
private void OnTextBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
MoveFocus(request);
}
}
EDIT
Alternatively, if you prefer using a Command, you can just set a KeyBinding in your TextBox :
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding YourCommand}" />
</TextBox.InputBindings>
And just put pretty much the same thing as above in your ICommandsExecute` logic.
Also, same question here: How do I simulate a Tab key press when Return is pressed in a WPF application?
I have a page containing two StackPanels, each containing one TextBox and one Button:
<StackPanel x:Name="Row1">
<TextBox x:Name="TextBox1" Text="" GotFocus="OnFocusHandler" LostFocus="OffFocusHandler"/>
<Button x:Name="Button1" Content="Convert" Click="OnClickHandler" Visibility="Collapsed"/>
</StackPanel>
<StackPanel x:Name="Row2">
<TextBox x:Name="TextBox2" Text="" GotFocus="OnFocusHandler" LostFocus="OffFocusHandler"/>
<Button x:Name="Button2" Content="Convert" Click="OnClickHandler" Visibility="Collapsed"/>
</StackPanel>
I would like to do the following:
When a textbox has focus, the other textbox must be hidden and the corresponding button must show
When a textbox is out of focus, we are back to the original display: only empty textboxes are visible
I don't want the button to be able to trigger the OffFocusHandler
This is the current code that I have for the three handlers:
private void OnFocusHandler(object sender, RoutedEventArgs e)
{
TextBox SenderTextBox = (TextBox)sender;
if (SenderPanel.Name == "TextBox1")
{
Button1.Visibility = Visibility.Visible;
}
else if (SenderPanel.Name == "TextBox2")
{
Button2.Visibility = Visibility.Visible;
}
}
private void OffFocusHandler(object sender, RoutedEventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
Button1.Visibility = Visibility.Collapsed;
Button2.Visibility = Visibility.Collapsed;
}
private void OnClickHandler(object sender, RoutedEventArgs e)
{
// some stuff unrelated to my issue
}
How do I avoid the button clicking to trigger the OffFocusHandler code?
Is there another way to code this? I'm a complete beginner so I may not think the right way.
You can just Bind to the TextBox.IsFocused property in Xaml, and use the BooleanToVisibilityConverter to show/hide the button.
Example:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="300" Width="400" Name="UI" >
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolTovisible" />
</Window.Resources>
<Grid>
<StackPanel x:Name="Row1" Height="54" VerticalAlignment="Top">
<TextBox x:Name="TextBox1" Text="" />
<Button x:Name="Button1" Content="Convert" Visibility="{Binding ElementName=TextBox1, Path=IsFocused, Converter={StaticResource BoolTovisible}}"/>
</StackPanel>
<StackPanel x:Name="Row2" Margin="0,60,0,0" Height="51" VerticalAlignment="Top">
<TextBox x:Name="TextBox2" Text="" />
<Button x:Name="Button2" Content="Convert" Visibility="{Binding ElementName=TextBox2, Path=IsFocused, Converter={StaticResource BoolTovisible}}"/>
</StackPanel>
</Grid>
</Window>
for each element, there is a Visibility tag, it is "Visible" by default but you can assign "Hidden" or "Collapsed" as follow:
<RadioButton Margin="20,118,318,-43" GroupName="MCSites" Visibility="Hidden">
Radio Button Description
</RadioButton>
Hi all I am trying to add ContexMenu into my Dynamically generated Tree.
Below is my Code For generating Tree.
I need to add ContexMenu for :
NEW:
EDIT:
DELETE
on MouseClick I should be able to perform the same operation of clicking the Respective Buttons.
Can any body help in completing the code.
XML:CODE
<Window x:Class="NewTree_DynamicNode.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Loaded="TestsTreeViewPageFunction_Loaded">
<Grid>
<TreeView Name="treeFileSystem" >
<TreeViewItem Header="Suite" Name="MYTree" Tag="hi" IsExpanded="True">
<TreeViewItem Name="treeFileSystem1" />
</TreeViewItem>
</TreeView>
<TextBox Name="textBox1" Height="23" HorizontalAlignment="Left" Margin="121,150,0,0" VerticalAlignment="Top" Width="120" />
<Button Content="New" Height="23" HorizontalAlignment="Left" Margin="12,121,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="New_Click" />
<Button Content="Edit" Height="23" HorizontalAlignment="Left" Margin="12,150,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="Edit_Click"/>
<Button Content="Copy" Height="23" HorizontalAlignment="Left" Margin="12,179,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="Copy_Click"/>
</Grid>
</Window>
C# CODE:
private void TestsTreeViewPageFunction_Loaded(object sender,
RoutedEventArgs e)
{
this.MYTree.Items.Clear();
for (int j = 1; j < 4; j++)
{
TreeViewItem Case = new TreeViewItem();
Case.Header = "Case "+j.ToString();
Case.IsExpanded = true;
Case.Items.Add(Trythis());
this.MYTree.Items.Add(Case);
}
}
private TreeViewItem Trythis()
{
TreeViewItem Step = new TreeViewItem();
for (int i = 0; i < 5; i++)
{
Step.Header = "Steps " + i.ToString();
}
return Step;
}
private void New_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "New Button Clicked";
}
private void Edit_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "Edit Button Clicked";
}
private void Copy_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "Copy Button Clicked";
}
EDIT:
I am looking for a solution where I should be able to add or limit the ContexMenu to TreeItem based on there Header information.
You can use style to set common ContextMenu property:
<TreeViewItem Header="Suite" Name="MYTree" Tag="hi" IsExpanded="True">
<TreeViewItem.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="New" />
<MenuItem Header="Edit" />
<MenuItem Header="Delete" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</TreeViewItem.Resources>
<TreeViewItem x:Name="treeFileSystem1" />
</TreeViewItem>
Or you can add context menu in code: before line this.MYTree.Items.Add(Case);
you can add something like this:
var menu = new ContextMenu();
menu.Children.Add(new MenuItem{Header = "Save"});
menu.Children.Add(new MenuItem{Header = "Load"});
Case.ContextMenu = menu;