I have created a WPF application. In that I need to show image on button . So that where should I store that image on my application and how to set source property of the image ??
Please replay
<Button x:Name="Button1" Width="200" Height="200" Content="Button1" Margin="0,0,0,400">
<Button.Background>
<ImageBrush **ImageSource ="Images/AERO.png"** ></ImageBrush>
</Button.Background>
</Button>
In CS File:( Store image in resources or any folder in Project Directory)
private void Button1_Click_1(object sender, RoutedEventArgs e)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("Images/AERO.png"));
Button1.Background = brush;
}
Add a image to your project.
Set this image's property : 'no copy' and 'Resource'
Add a Button,remove it's default 'Content' property.
Add a Image control as this Button's content.
<Grid>
<Button x:Name="button" HorizontalAlignment="Left" Margin="190,136,0,0" VerticalAlignment="Top" Width="104" Height="42">
<Image Source="pen.png" />
</Button>
</Grid>
You can add image on a button like,
<Button>
<StackPanel>
<Image Source="Images/imageName.png" />
</StackPanel>
</Button>
Or else you can try,
<Window.Resources>
<ImageBrush x:Key="MyResource" ImageSource="Images/imageName.png" />
</Window.Resources>
<Grid>
<Button Background="{StaticResource MyResource}"/>
</Grid>
Else try this.
<Window.Resources>
<ImageBrush x:Key="MyResource" ImageSource="Images/imageName.png" />
</Window.Resources>
<Grid>
<Button>
<Button.Content>
<TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="34" Height="19" Margin="0">
<TextBlock.Background>
<StaticResource ResourceKey="MyResource"/>
</TextBlock.Background>
</TextBlock>
</Button.Content>
</Button>
</Grid>
Add image to project and set the image properties:
Build Action: Resource
Copy to Output Directory: Do not copy
Then in the .xaml file add a Button (for example):
<Button HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" Width="40" Height="40">
<Image Source="Resources/yourImageName.png" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Button>
Related
I have a Custom tooltip icon which also has its content bound to Datacontext. My need was to open Tooltop on mouse hover as well as on click event. So I used following code
<Image Source="/MainUI;component\Images\home\tooltip_info.png"
Width="24" Height="24" Stretch="Fill" HorizontalAlignment="Right"
Name="ImageToolTip"
Margin="0,0,0,0" MouseUp="ImageToolTip_MouseUp" MouseLeave="ImageToolTip_MouseLeave">
<Image.ToolTip>
<ToolTip BorderBrush="Transparent" Background="Transparent" HorizontalOffset="-142">
<TextBlock TextWrapping="WrapWithOverflow"
Style="{StaticResource ExcalFont-MSFD300}"
FontSize="14" Text="{Binding Tips}"
Width="300" Padding="15,15,15,15">
<TextBlock.Background>
<ImageBrush ImageSource="Images\home\popupbox.png" />
</TextBlock.Background>
</TextBlock>
</ToolTip>
</Image.ToolTip>
</Image>
Code Behind:
private void ImageToolTip_MouseUp(object sender, MouseButtonEventArgs e)
{
((ToolTip)((FrameworkElement)sender).ToolTip).IsOpen = true;
}
private void ImageToolTip_MouseLeave(object sender, MouseEventArgs e)
{
((ToolTip)((FrameworkElement)sender).ToolTip).IsOpen = false;
}
Now the issue is on mouse up It opens Tooltip but it does not bind the text.
This is working fine if I am using static text instead of Binding. What am I doing wrong?
In case I mouse hover on icon then it works fine and shows the content too. Thereafter everytime mouse click also shows the tooltip content. Not sure why mouse click do not work initially. –
ToolTip is not part of the VisualTree.
A problem that is similar to the problem you have is described here:
RelativeSource binding from a ToolTip or ContextMenu
One option to solve your problem would be something like this:
<Image Source="/MainUI;component\Images\home\tooltip_info.png"
Width="24" Height="24" Stretch="Fill" HorizontalAlignment="Left"
Name="ImageToolTip"
Margin="0,0,0,0" MouseUp="ImageToolTip_MouseUp" MouseLeave="ImageToolTip_MouseLeave" Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=Self}}">
<Image.ToolTip>
<ToolTip BorderBrush="Transparent" Background="Transparent" HorizontalOffset="-142" >
<TextBlock TextWrapping="WrapWithOverflow"
FontSize="14" Text="{Binding PlacementTarget.Tag.Tips,
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ToolTip}}"
Width="300" Padding="15,15,15,15">
<TextBlock.Background>
<ImageBrush ImageSource="Images\home\popupbox.png" />
</TextBlock.Background>
</TextBlock>
</ToolTip>
</Image.ToolTip>
</Image>
I have a windows with this controls.
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
///elemnts in row 0
<StackPanel Grid.Row="1">
<ToolBar x:Name="Toolbar">
<Label Content="TextPattern" />
<ComboBox></ComboBox>
<Button Command="{Binding ScanCommand}">
<Image Source="/Images/sacn.png"></Image>
</Button>
<Button Command="{Binding FaxCommand}" x:Name="FaxButton" PreviewKeyDown="FaxButton_PreviewKeyDown">
<Image Source="/Images/fax.png"></Image>
</Button>
</ToolBar>
<TextBox Width="{Binding ElementName=Toolbar,Path=ActualWidth}" x:Name="BodyTextBox" Focusable="True" TextWrapping="Wrap" AcceptsReturn="True" Text="{Binding Body}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="2" >
<Button Content="Save" Command="{Binding SaveCommand}"></Button>
<Button Content="Cancel" Command="{Binding CancelCommand}"></Button>
</StackPanel>
</Grid>
When i use tab key for get focus, it go to savebutton after press tab of faxbutton. But i want to go to BodyTextbox.
I use PreviewKeyDown for set focus.
private void FaxButton_PreviewKeyDown(object sender, KeyEventArgs e)
{
if(e.Key==Key.Tab)
{
BodyTextBox.Focus();
BodyTextBox.SelectAll();
FocusManager.SetFocusedElement(this, BodyTextBox);
}
}
But don't set Focus.
When you load UserControl or Windowcontrol:
Please write this in the UserCOntrol_Load Event:
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Input,
new Action(delegate ()
{
BodyTextBox.Focus(); // Set Logical Focus
Keyboard.Focus(BodyTextBox); // Set Keyboard Focus
}));
it will be automatically focus n also u can write content from keyboard without any tab pressing..
Hope this will work for U..
Thanks,
Praveen
Remove the PreviewKeyDown event and Add KeyboardNavigation.TabNavigation="Continue" in tool bar
<ToolBar x:Name="Toolbar" KeyboardNavigation.TabNavigation="Continue">
I'm new to Xaml and WPF . I'm using the following code to extract botton title and image from RSS feeds .
The problem that the botton react only when user click on the border ... it dosen't react when user click on text or image .
<ListBox.ItemTemplate >
<DataTemplate >
<Button Background="{Binding C:AccentColors}" Width="400" Height="100" HorizontalAlignment="Left" >
<Grid Width="400" Height="100" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Image Source="{Binding XPath=enclosure/#url}" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" />
<TextBlock TextWrapping="Wrap" Text="{Binding XPath=title}" FontWeight="Bold" Grid.Column="2"/>
</Grid>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You have to add the Click event in your button.
<Button Background="{Binding C:AccentColors}" Width="400" Height="100" HorizontalAlignment="Left" Click="MethodNameHere".....
and also you have to create and implement the method in the Window C# file
The common mistake is that somebody placed TextBlock on top of button without realizig it, because TextBlock has transparent Background.
Visually, the Text of TextBlock may be outside Button, but since TextBlock alignment is set to strech by default, it fills entire area.
hard to say if this is your case...
<Grid>
<Button Content="I'm not working" Margin="0,100,0,0" />
<TextBlock Text="I'm in top left corner" />
<Grid />
if you set TextBlock.Background to Red, you entire grid would be red.
The button is taking the clicks. You need a handler, either a command binding or if you're not using MVVM - a code behind method:
<Button Click="ButtonBase_OnClick"
Handler will look like this:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
}
I am new to Silverlight, i have given a task to create a print preview template for a GIS Web Application for my company. My requirement is as follows:
1) When i click on the print preview button on my mainpage it should popup a new print preview window
2) The preview window should have mainpage content
3) It should have a print button to print it.
4) It should have next page and previous page option
5) A zoom icon/slider to zoom in and zoom out the preview contents
Upto now i have just a added a preview button on my mainpage, which on click opening a new xaml window and on that window i have set contents like this.
Here is the view of PrintPreview.xaml
<Grid x:Name="LayoutRoot" Background="White">
<Canvas x:Name="m_canvas" Height="490" VerticalAlignment="Top" Margin="0,0,60,0">
<Button x:Name="m_btn_print" Canvas.Top="5" Canvas.Left="5" Width="32" Height="32" Click="m_btn_print_Click">
<Image Source="Images/print.png" Stretch="Fill" />
<ToolTipService.ToolTip>
<ToolTip Content="Print"></ToolTip>
</ToolTipService.ToolTip>
</Button>
<Button x:Name="m_btn_prev_page" Canvas.Top="5" Canvas.Left="45" Width="32" Height="32" Click="m_btn_prev_page_Click">
<Image Source="Images/arrow.left.png" Stretch="Fill"/>
<ToolTipService.ToolTip>
<ToolTip Content="Previous Page"></ToolTip>
</ToolTipService.ToolTip>
</Button>
<Button x:Name="m_btn_next_page" Canvas.Top="5" Canvas.Left="85" Width="32" Height="32" Click="m_btn_next_page_Click">
<Image Source="Images/arrow.right.png" Stretch="Fill"/>
<ToolTipService.ToolTip>
<ToolTip Content="Next Page"></ToolTip>
</ToolTipService.ToolTip>
</Button>
<Slider x:Name="m_sld_size" Minimum="50" Maximum="125" Value="100" SmallChange="5" LargeChange="5" ValueChanged="m_sld_size_ValueChanged"
Canvas.Top="15" Canvas.Left="245" VerticalAlignment="Top" HorizontalAlignment="Right" Width="80" />
<TextBlock x:Name="m_lbl_size" Canvas.Top="2" Canvas.Left="265" FontSize="9" Text="Zoom" Foreground="Black" />
<Canvas x:Name="m_canvas_print" Canvas.Top="40" Canvas.Left="5" Width="317" Height="445" Background="White"
VerticalAlignment="Top" HorizontalAlignment="Left">
</Canvas>
</Canvas>
</Grid>
Now what should be my code behind, how should i show the mainpage.xaml content on this preview. Please help its urgent
Thanks in advance.
My first word of advise is not to use a canvas object unless you are filing it in with something that is the same every time you print it. If you are printing dynamic data values that can change based on the time it is printed I would move to a stack panel or grid.
That being said you will need to wire up the print function to print the m_cavas_print object when a user presses the print button. I have added some sample print code below.
PrintDocument pd = new PrintDocument();
pd.PrintPage += (s, e) =>
{
e.PageVisual = m_cavas_print;
};
pd.Print("MainPageContent");
This is the basic principle behind printing. You can put this in your print button click event handler and it will print whatever is in the canvas. I used a lambda expression here to handle the print page event but you can use a separate method as well.
I am using a ImageButton as
<Button Width="80" Height="25"
VerticalAlignment="Top"
HorizontalAlignment="Right"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Margin="0,0,1.5,0"
Name="btnActivate"
Click="OnActivate">
<StackPanel Orientation="Horizontal" Margin="3">
<Image Source="Shutdown.ico"
Width="12" Height="12"
Margin="0,0,5,0"/>
<TextBlock>Activate</TextBlock>
</StackPanel>
</Button>
I want to change the Content of button to 'Deactivate' when I click on it without changing the image. How can I do that? And also I need to some operations based on the content of button in my C# code.
You could just add a name to the TextBlock
<TextBlock Name="textBlock1">Activate</TextBlock>
and use it to modify the content in your OnActivate event handler
textBlock1.Text = "Deactivate";