When I click image it displayed the menu but icon's are not displayed. I tried in two ways:
One is I resized the icon that's not working
Second one is I set the path using icon property that's not working.
What's the way to set the icon for context menu items??
Xaml:
<Image Height="20" Width="20" Source="/CitiCall.WinClient;component/Images/user_icon.png" MouseDown="image1_MouseDown" Margin="0,0,4,6" HorizontalAlignment="Right" Name="image1" Stretch="Fill" VerticalAlignment="Top">
<Image.ContextMenu>
<ContextMenu>
<MenuItem Header="Reset password" Icon="/CitiCall.WinClient;component/Images/reset.png"/>
<!--<MenuItem.Icon>
<Image Source="/CitiCall.WinClient;component/Images/reset.png" ></Image>
</MenuItem.Icon>
</MenuItem>-->
<MenuItem Header="Edit Profile"/>
<MenuItem Header="Settings"/>
<MenuItem Header="About us"/>
</ContextMenu>
</Image.ContextMenu>
</Image>
Xamal.cs:
private void image1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
Image image = sender as Image;
ContextMenu contextMenu = image.ContextMenu;
contextMenu.PlacementTarget = image;
contextMenu.IsOpen = true;
}
}
Actually it should work if you write :
<MenuItem.Icon>
<Image Source="Images/reset.png" ></Image>
</MenuItem.Icon>
Just take care of right clicking to the properties of the images in your project, set it as Content, and Copy if newer.
Have a look at : WPF image resources
Regards
This worked for me:
<Button.ContextMenu>
<ContextMenu>
<MenuItem Command="{Binding BringToFront}" ToolTip="Bring to front.">
<MenuItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="{StaticResource Images.TextEditIcon}" Height="14" Width="14" Margin="-20 0 0 0"/>
<TextBlock>Bring to Front</TextBlock>
</StackPanel>
</MenuItem.Header>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
For some reason, using <MenuItem Icon="..."> did not work.
And in the resource dictionary:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:presentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
<BitmapImage x:Key="Images.TextEditIcon" UriSource="../Images/TextEditIcon.png" presentationOptions:Freeze="True" />
</ResourceDictionary>
You'll need to include the image in your project, and set the type to "Resource" in properties. You'll also need to include this resource dictionary somewhere. I'm not going to lie - images are a real pain to set up in WPF. But once they are set up, it's very reliable.
If this image does not work, do not bother troubleshooting it directly on the ContextMenu. Instead, try something like this in a simple StackPanel or Grid:
<Image Source="{StaticResource Images.TextEditIcon}" Height="14" Width="14"/>
Once this is displaying, then you can add it into the ContextMenu.
Using c# code behind you can set the icon with a Path syntax using Path Markup Syntax
// paths for each menu item icon ...
string miniPath1 = "M0,0 L8,0 8,1 8,2 0,2 0,1 z";
string miniPath2 = "F1 M 34,17L 43,17L 43,23L 34,23L 34,17 Z M 35,19L 35,22L 42,22L 42,19L 35,19 Z";
MenuItem mi = new MenuItem { Header = "Menu Item Name", Tag = "My Item" };
Brush brush = Brushes.Black;
mi.Items.Add(new MenuItem() { Header = "Item1", Icon = ConvertPathToImage(miniPath1, brush) });
mi.Items.Add(new MenuItem() { Header = "Item2", Icon = ConvertPathToImage(miniPath2, brush) });
ContextMenu cm = new ContextMenu();
cm.Items.Add(mi);
Using a simple conversion:
private Image ConvertPathToImage(string PathPath, Brush brush)
{
Geometry gp = Geometry.Parse(PathPath);
GeometryDrawing gd = new GeometryDrawing(brush, new Pen(brush, 1.0), gp);
DrawingImage di = new DrawingImage { Drawing = gd };
return new Image() { Source = di };
}
Related
I have a foreach loop that add UserControl into a stackPanel.
The problem is that I need the height of the UserControl, because it's not the same for all userControl.
Note: I have to add the userControl on the grid after having his Height.
Here is what I tried :
foreach(Verset verset in sourate.versets)
{
Paper_Usercontrol.UC_Paper_Vers uc_verset = new Paper_Usercontrol.UC_Paper_Vers(verset);
uc_verset.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));
uc_verset.Arrange(new Rect(new System.Windows.Point(0, 0), uc_verset.DesiredSize));
uc_verset.UpdateLayout();
totalSize += Convert.ToInt32(uc_verset.ActualHeight);
// ... }
That give code give the value on ActualHeight of "99.85". I don't have any idea of what that value can be, it can't be the real one because it is the same value everytime, but when it show the userControl they have different size.
The other I tested is to add the UserControl on a temporary grid before having his height :
foreach(Verset verset in sourate.versets)
{
Paper_Usercontrol.UC_Paper_Vers uc_verset = new Paper_Usercontrol.UC_Paper_Vers(verset);
Grid_InitElement.Children.Add(uc_verset);
uc_verset.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));
uc_verset.Arrange(new Rect(new System.Windows.Point(0, 0), uc_verset.DesiredSize));
uc_verset.UpdateLayout();
totalSize += Convert.ToInt32(uc_verset.ActualHeight);
Grid_InitElement.Children.Clear();
// ... }
This code give me the Height of 705; it is the height of the Grid_InitElement, again don't ask me why I don't know why it do that
Here is an image if I let all the userControl on the temporaryGrid (to show u they have different size) :
enter image description here
Here is my userControl WPF :
<UserControl x:Class="Zone_Muslim.Paper_Usercontrol.UC_Paper_Vers"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Zone_Muslim.Paper_Usercontrol"
mc:Ignorable="d"
>
<StackPanel x:Name="StackPanel">
<TextBlock x:Name="ar" TextAlignment="Right" TextWrapping="Wrap" Margin="10,0,10,0">
</TextBlock>
<Separator Height="10" Width="0"></Separator>
<TextBlock x:Name="ph" TextAlignment="Justify" TextWrapping="Wrap" Margin="10,0,10,0">
</TextBlock>
<Separator Height="10" Width="0"></Separator>
<TextBlock x:Name="fr" TextAlignment="Justify" TextWrapping="Wrap" Margin="10,0,10,0"/>
<Separator Height="20" Width="0"></Separator>
</StackPanel>
ps: the Text of the TextBlock is set on the Initialization of the UserControl, and it's that text that determine the size of the UserControl :
public UC_Paper_Vers(Verset verset)
{
InitializeComponent();
ar.Text = verset.text_arabe;
ph.Text = verset.text_phonetique;
fr.Text = verset.position_ds_sourate + ". " + verset.text;
}
Please help me find a solution to have the actualHeight of my userControl
Thanks
I have a ListView in my UWP app with an ItemTemplate that consists of a Path geometry and a TextBox:
<ListView
x:Name="ListView"
CanDragItems="True"
CanReorderItems="True"
ItemsSource="{x:Bind Categories}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="myData:Category">
<StackPanel
Orientation="Horizontal"
ToolTipService.ToolTip="{x:Bind CategoryString, Mode=OneWay}">
<Path
Margin="14, 10, 4, 0"
Width="10"
Height="10"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Data="{x:Bind SymbolGeometry, Mode=OneWay}"
Fill="{x:Bind Colour, Mode=OneWay}"/>
<TextBox
BorderThickness="0"
Background="Transparent"
Text="{x:Bind LegendLabel, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
As part of exporting a piece of the UI to an SVG I would like to find the locations (in pixel coordinates relative to any parent item) of the TextBox text and Path geometry for each item in the ListView. Does anybody know how to achieve this? As the ListView items are dependant on user input I'm unsure how to retrieve the necessary information.
I'm aware UIElements can be converted to bitmaps for export, however this does not fulfil the requirements of the app.
You could use the UIElement.TransformToVisual(UIElement) Method and GeneralTransform.TransformPoint(Point) Method to get the locations of UIElement objects.
Please check the following code:
for(int i=0;i<ListView.Items.Count;i++)
{
var item = ListView.ContainerFromIndex(i) as ListViewItem;
var path = VisualTreeHelper.GetChild(item.ContentTemplateRoot as DependencyObject, 0) as Windows.UI.Xaml.Shapes.Path;
var box = VisualTreeHelper.GetChild(item.ContentTemplateRoot as DependencyObject, 1) as TextBox;
var visual1 = path.TransformToVisual(ListView); //Select the ListView control as the parent element.
var point = visual1.TransformPoint(new Point(0, 0));
var visual2 = box.TransformToVisual(ListView);
var point2 = visual2.TransformPoint(new Point(0, 0));
}
i'm have some trouble with tooltip in textblock wpf .
After i completed 1 task and if task is error i want update error info with tooltip . But tooltip never show when completed . Please help me .
Thanks
Here my code
C# code
if (status == "Error")
{
LogCreateSite item = (LogCreateSite)gridLog.Items[rowIndex];
item.ErrorInfo = "Error";
DataTemplate template = cellTitle.ContentTemplate;
Canvas canvas = (Canvas)template.LoadContent();
TextBlock txtError = (TextBlock)canvas.Children[1];
ToolTip toolTip = new ToolTip();
toolTip.Content = "asdfasdf";
txtError.ToolTip = toolTip;
txtError.UpdateLayout();
}
And my Xaml :
<DataTemplate x:Key="error">
<Canvas Margin="10,15,0,0">
<!--<Ellipse Fill="#FF5050" Width="12" Height="12">
</Ellipse>-->
<Viewbox Width="16" Height="16">
<Frame Source="../Icon/Error_16.xaml" />
</Viewbox>
<TextBlock Text="Error" Margin="25,-3,0,0">
</TextBlock>
<TextBlock Cursor="Hand" Name="txtErrorInfo" ToolTip="{Binding ErrorInfo, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" FontSize="14" Text="?" Margin="60,-3,0,0" FontWeight="Bold" Foreground="Blue">
</TextBlock>
</Canvas>
</DataTemplate>
You need to do binding for tool tip to show the error/message to user.
Please go thru this tutorial for wpf binding. Introduction to WPF data binding from WPF-tutorial.
your XAML should be like this for a proper binding.
Name="txtErrorInfo" ToolTip="{binding path=error mode=OneWay UpdateSourceTrigger=PropertyChanged}"
It is required to mention mode and UpdateSourceTrigger when you are changing the property which should be shown to user.
Correcting your code, see this sample for how to show ToolTip from code :
private void Button_Click_1(object sender, RoutedEventArgs e)
{
ToolTip t = new ToolTip();
t.Content = DateTime.Now.ToString();
t.IsOpen = true;
t.PlacementTarget = txtError;
t.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
txtError.ToolTip = t;
}
I want to change image dynamically on a button. I think I have what is required to find the image. I have som resourcedictionary for the buttons here:
<BitmapImage x:Key="Mark1Empty" UriSource="Marks/Tom1.png"/>
<BitmapImage x:Key="Mark1Chosen" UriSource="Marks/Ny1.png"/>
Then afterwards, I create the button like this in XAML User control:
<Button x:Name="Mark1Button"
Height="30" Width="40" Template="{StaticResource Mark1ButtonTemplate}"
cal:Message.Attach="[Event Click] = [Action SayHello($source, $this)]"/>
The ControlTemplate looks like this:
<ControlTemplate x:Key="Mark1ButtonTemplate" TargetType="{x:Type Button}">
<Image Name="Mark1ButtonImage" Source="{StaticResource Mark1Empty}" />
</ControlTemplate>
After that. In C# code when I press the image. Then it triggers, but the image doesn't change. It is blank after clicking on the image.
public void SayHello(object sender, object kravsource)
{
var selectedButton = sender as Button;
if (selectedButton != null)
{
Image image = (Image)selectedButton.Template.FindName("Mark1ButtonImage", selectedButton);
BitmapImage imagePicker = (BitmapImage)Application.Current.FindResource("Mark1Chosen");
image.Source = new BitmapImage(new Uri(imagePicker.UriSource.ToString(), UriKind.RelativeOrAbsolute));
image.Stretch = Stretch.Uniform;
}
}
I had a similar question here: Input dialogue popup on mouse click
So when the user clicks on the canvas, a colored circle is marked where the mouse clicked, and a popup is displayed with two textboxes to get user input. I'm trying to accomplish two things: Instead of a popup, I would like to have a window displayed so the user can move the popup around (right now it's just a white space that sits in the same place). I also would like to add an 'ok' button so that when it's clicked, the two inputs get saved to their respective variables and the window closes.
I've tried adding a tag after the popup, but I get a xamlparseexception. I'm not sure what to do to make it so the popup is a window. Regarding the input text, I've seen many examples where the user enters text into a textbox and the data is saved to a variable, but nothing on saving it after the textbox window is closed. This is my first wpf application and I'm slowly trying to work through and learn it. Here's the code I have as it currently stands:
XAML:
<Window x:Class="CanvasStuff.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" Height="410" Width="869">
<Grid Height="387">
<Label Content="Image" Height="32" HorizontalAlignment="Left" Margin="11,10,0,0"
Name="selectedFileName" VerticalAlignment="Top" Width="137"
Background="LightGray" BorderBrush="Gray" BorderThickness="1"/>
<Button Content="Browse File" Height="34" HorizontalAlignment="Left" Margin="154,6,0,0"
Name="BrowseButton" VerticalAlignment="Top" Width="119"
Foreground="Maroon" FontSize="16" FontFamily="Georgia" Click="BrowseButton_Click" />
<Button Content="Input Range and Heading" Height="34" HorizontalAlignment="Left" Margin="279,6,0,0"
Name="InputRangeBearing" VerticalAlignment="Top" Width="191"
Foreground="Maroon" FontSize="16" FontFamily="Georgia" Click="InputButton_Click" />
<Canvas Margin="0,45,2,8" x:Name="canvas1" MouseDown= "addNode_MouseDown">
<Popup Name="inputPopup" MouseDown="addNode_MouseDown" >
<Grid Height="150" Background="White" >
<Label Content="Range to object (m): " Height="28" HorizontalAlignment="Left" Margin="39,28,0,0" Name="label1" VerticalAlignment="Top" />
<TextBox x:Name="rangeToObject" Height="23" HorizontalAlignment="Left" Margin="151,30,0,0" VerticalAlignment="Top" Width="120" />
<Label Content="Heading to Object (0-360): " Height="28" HorizontalAlignment="Left" Margin="39,63,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox x:Name="headingToObject" Height="23" HorizontalAlignment="Left" Margin="151,68,0,0" VerticalAlignment="Top" Width="120" />
</Grid>
</Popup>
</Canvas>
</Grid>
</Window>
Code behind:
namespace CanvasStuff
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = "c:\\";
dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string selectedFileName = dlg.FileName;
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(selectedFileName));
canvas1.Background = brush;
BitmapImage bitmap = new BitmapImage();
}
}
private void InputButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Please click on known object to enter range and heading of that object.");
}
private void addNode_MouseDown(object sender, MouseButtonEventArgs e)
{
Point currentPoint = new Point();
if (e.ButtonState == MouseButtonState.Pressed)
currentPoint = e.GetPosition(this);
Ellipse ellipse = new Ellipse();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
ellipse.Fill = mySolidColorBrush;
ellipse.Width = 10;
ellipse.Height = 10;
Canvas.SetLeft(ellipse, e.GetPosition(canvas1).X);
Canvas.SetTop(ellipse, e.GetPosition(canvas1).Y);
canvas1.Children.Add(ellipse);
inputPopup.IsOpen = true;
}
}
}
To clarify, are you saying you'd prefer the popup box to appear as a completely separate window that the user can move around?
It's hard to know how deep to go, if this is your first WPF application. But it looks to me like you're straying into territory where MVVM and Databinding will be a great help to you.
I can strongly recommend that you spend some time getting to grips with the MVVM pattern, and WPF's databinding paradigm. There are plenty of good tutorials out there.
Once you've got the hang of the basics, I can highly recommend the MVVM-Light Toolkit to ease the pain of creating ViewModels and ICommands. It also contains a baisc messaging service, to simply communication between Windows and Views.
Sorry, I've not actually answered your question - but hopefully the links will give you some help :)