i'm a newbie in DOT NET
I'm building a WPF App using C# in which I have a MenuItem (Checkable) for toggling the Word Wrap feature on and of for a TextBox(Just like in Notepad) i.e to switch between TextWrapping="Wrap" and TextWrapping="NoWrap"
MenuItem :
<MenuItem Header="_Word Wrap" Name="wordWrap" IsCheckable="True" IsChecked="True" Checked="wrap_Click"/>
TextBox:
<TextBox TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" Name="txtContent">
How to create a function so that , when the MenuItem is checked, it turns the Word wrap feature on and when the MenuItem is unchecked, it turns the Word wrap feature off
I tried the following piece of code ,
private void wrap_Click(object sender, RoutedEventArgs e)
{
if (wordWrap.IsChecked)
txtContent.TextWrapping = TextWrapping.Wrap;
else
txtContent.TextWrapping = TextWrapping.NoWrap;
}
but it shows an Exception at txtContent.TextWrapping = TextWrapping.Wrap; :
Object reference not set to an instance of an object.
Please help me
Thank you !
This doesn't seem to be a problem with your actual event. Just try changing your event to a click event.
So, change this:
<MenuItem Header="_Word Wrap" Name="wordWrap" IsCheckable="True" IsChecked="True" Checked="wrap_Click" />
To this:
<MenuItem Header="_Word Wrap" Name="wordWrap" IsCheckable="True" IsChecked="True" Click="wrap_Click" />
This solved the problem for me. You don't even have to change the event code. It is correct. For some reason, the compiler just didn't like having it as a checked event...
Related
There is an application I want to make using C#. This is part of it's UI:
The white area below the menu bar represents a TreeView. I want the Tree_View menu to appear everywhere in the white area when right-clicking with the mouse.
This is the XAML code for the Tree_View object (mind the foo function there):
<MenuItem Name="Menu_Tree" Header="_Tree_View">
<MenuItem Header="_New_Scene" IsCheckable="false" Click = foo/>
<MenuItem Header="_Copy_This_Scene" IsCheckable="false"/>
<MenuItem Header="_Remove_This_Scene" IsCheckable="false"/>
<Separator />
<MenuItem Header="_New_Shot" IsCheckable="false"/>
<MenuItem Header="_Copy_This_Shot" IsCheckable="false"/>
<MenuItem Header="_Remove_This_Shot" IsCheckable="false"/>
<Separator />
<MenuItem Header="_Move_This_Shot_Up" IsCheckable="false"/>
<MenuItem Header="_Move_This_Shot_Down" IsCheckable="false"/>
</MenuItem>
and this is the XAML code for the Tree_View object:
<Grid Name="TreeHolder" Column="0" Margin="20,10,10,10" Background="DimGray">
<TreeView Name="myTree" MouseRightButtonDown="something" ToolTip="Right Click to Add or Remove Scenes and Scots.">
</TreeView>
</Grid>
This is the something function which should be triggered by right-click:
private void something(object sender, MouseButtonEventArgs e)
{
ContextMenu cm = new ContextMenu();
//cm.Items.Add("Add a New Scene ?", ... );
Menu_Tree.ContextMenu = cm;
}
Since nothing works there, I want to ask the following:
a) How to make the Tree_view menu items appear in the Tree_View object white area as well?
b) If so, how will I make it an enabled menu, for example being able to trigger the foo function?
(In other worlds, make an exact copy of the menu list, make it visible with right click and make it work as well)
I strongly believe that it has to do with the ContextMenu which I am not able to use fairly, so any help would be highly appreciated.
The items to add are MenuItems. You can create each item as you would with any other object, set its Header property and assign a Click event handler. The sender is the TreeView itself.
private void something(object sender, MouseButtonEventArgs e)
{
var cm = new ContextMenu();
var newSceneMenuItem = new MenuItem { Header = "_New_Scene" };
newSceneMenuItem.Click += OnNewSceneClick;
cm.Items.Add(newSceneMenuItem);
var treeView = (TreeView) sender;
treeView.ContextMenu = cm;
}
private void OnNewSceneClick(object sender, RoutedEventArgs e)
{
MessageBox.Show("I am a Message Box.", "New Scene clicked");
}
However, you can do this much easier in XAML without the right click handler.
<Grid Name="TreeHolder" Column="0" Margin="20,10,10,10" Background="DimGray">
<TreeView Name="myTree" ToolTip="Right Click to Add or Remove Scenes and Scots.">
<TreeView.ContextMenu>
<ContextMenu>
<MenuItem Header="_New_Scene" Click="foo"/>
<MenuItem Header="_Copy_This_Scene"/>
<MenuItem Header="_Remove_This_Scene"/>
<Separator />
<MenuItem Header="_New_Shot"/>
<MenuItem Header="_Copy_This_Shot"/>
<MenuItem Header="_Remove_This_Shot"/>
<Separator />
<MenuItem Header="_Move_This_Shot_Up"/>
<MenuItem Header="_Move_This_Shot_Down"/>
</ContextMenu>
</TreeView.ContextMenu>
</TreeView>
</Grid>
You do not need to set the IsCheckable to false, that is already the default value. Instead of assigning a Click event handler you could use commands, but I guess this would be a too advanced topic for you question, since it does not look like you are employing the MVVM pattern.
I have a WPF window with 4 read-only TextBoxes in it, to all of which I need to enable a context menu with copy option. Currently I'm doing with code behind. But I heard it is not a good approach.
<TextBox Name="StepsTextBox"
Text="{Binding Steps, Mode=OneWay}"
IsReadOnly="True"
Click="Copy_click"/>
Code-Behind:
private void Copy_click(object sender, RoutedEventArgs e)
{
StepsTextBox.Copy();
}
I tried using MVVM as follows:
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Copy" Command="{Binding OnCopyButtonClick}" CommandParameter="{Binding ElementName=StepsTextBox}"/>
</ContextMenu>
</TextBox.ContextMenu>
But how do I access this text box from the code if I pass it as parameter. And also how can I keep this code generic for all the textboxes?. Could anyone help?. Thanks in advance.
private void OnCopyButtonClick(TextBox textBox)
{
//??
}
You can use the build-in ApplicationCommands.Copy. No need to implement anything, the copy functionality is already implemented.
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Copy" Command="Copy" />
</ContextMenu>
</TextBox.ContextMenu>
You will still have to select the text before copying it, but that's to be expected when copying text.
I am learning c# WPF application.
I have a menu which has a dropdown list. In the dropdown there are 3 other items.
Now on clicking an item i want to display the form in the same window.
What i did is that i have created a new wpf form and navigated it from the previous page.
But now i want to open it in the same page itself.
Please help
XAML:
<Grid>
<DockPanel Height="43" HorizontalAlignment="Stretch" Name="dockPanel1"
VerticalAlignment="Top">
<Menu Height="47" Name="menu1" Width="Auto" DockPanel.Dock="Top"
VerticalAlignment="Top" HorizontalAlignment="Stretch" >
<MenuItem Header="_Entry Form" >
<MenuItem Header="_Student Details" Name="studentDetails" Click="studentDetails_Click_1"/>
<MenuItem Header="_Faculty Details" Name="facultyDetails" />
<Separator/>
<MenuItem Header="E_xit" Name="exit" />
</MenuItem>
</Menu>
</DockPanel>
CS
private void studentDetails_Click_1(object sender, RoutedEventArgs e)
{
// to navigate to a new page
Student std = new Student();
this.Close();
std.Show();
}
Now i donot want to navigate just display the student page in the same window
Firstly, you'll need to add XAML that represents the Student class, probably a bunch of TextBoxes, wrapped in some kind of container, like a Border, Grid, or StackPanel, and set the Visibility property of the container to Collapsed.
When the button is then clicked, you can populate the text boxes (or other UI elements) with the appropriate Student parameters, then set the container's Visibility property to Visible.
I would really recommend you check out the Model-View-ViewModel (MVVM) pattern, though. There is a bit of a learning curve, but it's well worth the investment.
I have a context menu attached to a button on a toolbar on one of my controls in WPF (.NET 4.0). The context menu has a style assigned to it in the XAML that defines the context menu. Left clicking on the button opens the button's context menu if it isn't opened already.
Here's the relevant XAML:
<Button x:Name="fileButton" Foreground="White" Margin="7, 0, -3, 0" VerticalAlignment="Stretch" MaxHeight="70" MaxWidth="78" MinHeight="55" MinWidth="62" Style="{DynamicResource ImageButton}" utils:WpfImageUtil.Image="{StaticResource fileButton}" Template="{DynamicResource GlassButton}" Content="File" Visibility="Visible" Click="fileButton_Click">
<Button.ContextMenu>
<ContextMenu Style="{DynamicResource ContextMenuStyle}">
<MenuItem x:Name="saveMenuItem" Header="Save" Click="saveMenuItem_Click" Style="{DynamicResource MenuItemStyle}" />
<MenuItem x:Name="saveDrawingMenuItem" Header="Save Drawing" Click="saveMenuItem_Click" Style="{DynamicResource MenuItemStyle}" />
<MenuItem x:Name="openMenuItem" Header="Open" Style="{DynamicResource MenuItemStyle}">
<MenuItem x:Name="openFromFile" Header="From File" Style="{DynamicResource MenuItemStyle}" />
<MenuItem x:Name="openFromDesktop" Header="From Desktop" Style="{DynamicResource MenuItemStyle}" />
</MenuItem>
<MenuItem x:Name="iconsMenuItem" Header="Icons" ItemsSource="{Binding}" Style="{DynamicResource MenuItemStyle}"/>
<MenuItem x:Name="prefsMenuItem" Header="Preferences" Style="{DynamicResource MenuItemStyle}"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
ContextMenuStyle is defined in a resource dictionary that is properly referenced.
When the context menu is opened with a left click, the style I have defined isn't applied to the menu, as shown below:
However, if the user right-clicks and opens the context menu the traditional way, the style is applied as expected:
Afterwards, left-clicking the button will show the style correctly:
I have been trying to figure this out for some time, but haven't been able to come up with any reason that this issue occurs. It seems like some kind of bug to me, but I'm not entirely sure. I also don't know what happens at the lower level when controls are right-clicked on that would cause the style to get applied correctly.
You should assign ContextMenu Style property in code (FindResource method msdn):
private void fileButton_Click(object sender, RoutedEventArgs e)
{
if (fileButton.ContextMenu.Style == null)
fileButton.ContextMenu.Style = this.FindResource("ContextMenuStyle") as Style;
fileButton.ContextMenu.IsOpen = true;
}
ContextMenu Overview (http://msdn.microsoft.com/en-US/library/ms742558.aspx)
A ContextMenu is attached to a specific control. The ContextMenu
element enables you to present users with a list of items that specify
commands or options that are associated with a particular control, for
example, a Button. Users right-click the control to make the menu
appear. ...
When you right-click on the control, style will be applied to the ContextMenu. So if you want to open ContextMenu in code, you should check if style is equal null and if it's true, you should assign appropriate style.
I don't know if this is possible but does anyone know if there is a way to put a user control inside a context menu, I have a dialog for adding items to a list but I would like when I click the add button for the control to be displayed in the context menu instead. (dont like having a lot of dialog's in my app).
Here is what I tried but it doesn't work:
<Button Grid.Column="2" Grid.Row="1" Content="Add Item" HorizontalAlignment="Right">
<Button.ContextMenu>
<ContextMenu>
<MenuItem>
<vm:MyControlView/>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
Is there a way to achieve this? (or something similar)
You could try this link, it looks like you might be able to extend ContextMenu like John Dunn did to meet your needs.