I'm reading through a WPF book, running through the examples.
Now, I'm working on some buttons.
Within the <Button.Content> </Button.Content> tag, I am able to define the text for the button, or I am able to define a shape that appears on the button face, but I don't appear to be able to do both within the same button.
Here's an example of the button with the ellipse shape on its face:
<Button Background="Blue">
<Button.Content>
<Ellipse Height="40" Width="40" Fill="Green" />
</Button.Content>
</Button>
Here's an example of the button with the text defined:
<Button Background="Red">
<Button.Content>
OK
</Button.Content>
</Button>
Here are the results of these two button definitions (within a WrapPanel):
When I attempt to create a button that defines both properties, using this code:
<Button Background="Orange">
<Button.Content>
OK
<Ellipse Height="40" Width="40" Fill="Wheat" />
</Button.Content>
</Button>
I receive an error that explains: The property 'Content' is set more than once.
So My question is: is it possible to combine these two button content definitions? The goal is to define a button that displays an ellipse shape and text.
You can only set the Content to one element, but you could do this by placing your elements inside of one container, e.g.:
<Button Background="Orange">
<Button.Content>
<Grid>
<Ellipse Height="40" Width="40" Fill="Wheat" />
<TextBlock Text="OK" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
</Button.Content>
</Button>
You are in fact setting the content twice, as you are trying to place two controls.
Try using a Stackpanel to hold both your controls. Also put the "OK" in a Label or Textbox
Example:
<Button Background="Orange">
<Button.Content>
<StackPanel Orientation="Horizontal">
<Label Content="OK" Background="Red"/>
<Ellipse Height="40" Width="40" Fill="Wheat" />
</StackPanel>
</Button.Content>
</Button>
I have added a web browser to my C# application using the following XAML code:
<WebBrowser Name="rivBrowser" Height="550" Width="620" Margin="0, 40, 0, 0" Visibility="Visible" />
<DockPanel>
<ToolBarTray DockPanel.Dock="Top">
<ToolBar Header="File">
<Button Command="New" Content="New" ToolBar.OverflowMode="Always" />
<Button Command="Open" Content="Open" ToolBar.OverflowMode="Always" />
<Button Command="Save" Content="Save" ToolBar.OverflowMode="Always" />
</ToolBar>
<ToolBar Header="Edit" Margin="5.4,0,-5.4,0">
<Button Command="Cut" Content="Cut" ToolBar.OverflowMode="Always" />
<Button Command="Copy" Content="Copy" ToolBar.OverflowMode="Always" />
<Button Command="Paste" Content="Paste" ToolBar.OverflowMode="Always" />
</ToolBar>
<ToolBar Margin="9.2,0,-8.2,0">
<Button Command="Back" ToolTip="Return to the previous page"/>
<Image Source="C:\Users\elgan\workspace\browser\riviam_windows\Images\navigateBack.png" Width="20" Height="20" Margin="0,0,0,2.4" />
</ToolBar>
<ToolBar Margin="16.4,0,-16.2,0" >
<Button Command="Forward" ToolTip="Proceed to the next page" />
<Image Source="C:\Users\elgan\workspace\browser\riviam_windows\Images\navigateForward.png" Width="20" Height="20" />
</ToolBar>
</ToolBarTray>
</DockPanel>
However, I'm getting some exceptions with the 'Forward' and 'Back' navigation buttons that I'm trying to add to the toolbar. The exceptions say:
Exception thrown
'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
and
Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '73' and line position '22'.
That line is the line <Button Command="Back" ToolTip="Return to the previous page"/>
It's the button I want to use to allow the user to navigate back to the previous page displayed in the browser- but I'm not sure that I'm doing this correctly... Should I be calling a method from my C# code here? How do I do that?
I've not used C# in about 6 years, so am not very familiar with it or how it works...
Since you don't say if you are using the MVVM pattern or not I'll assume you don't.
In this case, you'll want to subscribe to the Click event of the Button rather than using a Command
For example :
<Button Click="GoForward_OnClick" ToolTip="Proceed to the next page" />
And on your code-behind (yourfile.xaml.cs) you'll have the method called when the button is clicked :
private void GoForward_OnClick(object sender, RoutedEventArgs e)
{
// Your code logic goes here
}
Your Command should be pointing to a DelegateCommand object on your viewmodel. Here is a sample:
View:
<Button Command="{Binding SaveCommand}" />
ViewModel:
public DelegateCommand SaveCommand { get; private set; }
public SampleViewModel()
{
SaveCommand = new DelegateCommand(Save);
}
private void Save()
{
// do something
}
If you want to use built in ApplicationCommands the you should use such syntax of Command like ApplicationCommands.Cut.
For example:
<Button Content="Cut" Command="ApplicationCommands.Cut"/>
<Button Content="Copy" Command="ApplicationCommands.Copy"/>
<Button Content="Paste" Command="ApplicationCommands.Paste"/>
MSDN has a good article about a list of available Application Commands:
CancelPrint - Gets the value that represents the Cancel Print command.
System_CAPS_pubpropertySystem_CAPS_static
Close - Gets the value that represents the Close command.
ContextMenu - Gets the value
that represents the Context Menu command.
...
I have a problem in resizing the main window of my app.xaml form: after resizing it, the objects (buttons,combo boxes, textboxes, etc) inside the form are being resized, hidden or moved to another place in the form. I just want to resize the main window not the other objects. in the other words, I want to unlink these two (main window & other objects) from each other. after doing that, I can resize main window and then, manually move the objects to their new place in the form. the code of the form is given below.
<Window x:Class="WPFClient.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="550" Width="834" MaxHeight="550" MaxWidth="834" MinHeight="550" MinWidth="834">
<Grid>
<Grid.Background>
<LinearGradientBrush>
<GradientStop Color="LightSlateGray" Offset="0"/>
<GradientStop Color="White" Offset="0.5"/>
<GradientStop Color="LightSlateGray" Offset="0.9"/>
</LinearGradientBrush>
</Grid.Background>
<Label x:Name="loginLabelUName" Height="25" HorizontalAlignment="Left" Margin="179,200,0,0" VerticalAlignment="Top" Width="70">User Name:
</Label>
<TextBox x:Name="loginTxtBoxUName" Height="23" Margin="277,200,313,0" VerticalAlignment="Top" />
<Label x:Name="loginLabelIP" HorizontalAlignment="Left" Margin="179,232,0,255" Width="70">Service IP:</Label>
<TextBox x:Name="loginTxtBoxIP" Margin="277,232,313,0" Text="41.235.135.104" Height="23" VerticalAlignment="Top" />
<Button x:Name="loginButtonConnect" Background="Transparent" Margin="277,0,313,222" Click="buttonConnect_Click" Height="23" VerticalAlignment="Bottom">Connect</Button>
<Label x:Name="loginLabelStatus" Height="44" VerticalAlignment="Bottom" FontFamily="Jokerman" FontSize="20" Foreground="White" HorizontalAlignment="Right" Width="167" Margin="0,0,40,71">Offline</Label>
<ComboBox x:Name="loginComboBoxImgs" HorizontalAlignment="Right" Margin="0,200,197,222" Width="98" Background="Transparent" />
<Label x:Name="loginLabelTitle" Height="57" FontFamily="Jokerman" FontSize="25" Foreground="White" Margin="16,16,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="293">WCF / WPF Chat App.</Label>
<Polyline x:Name="loginPolyLine" StrokeThickness="2" Stroke="White" Points="140,90 140,300 700,300 700,90 140,90" Margin="-9,42,116,165" />
<ListBox x:Name="chatListBoxMsgs" Margin="10,62,167,84" />
<ListBox x:Name="chatListBoxNames" HorizontalAlignment="Right" Margin="0,62,10,84" Width="139" />
<CheckBox x:Name="chatCheckBoxWhisper" Height="15" HorizontalAlignment="Right" Margin="0,37,10,0" VerticalAlignment="Top" Width="120" Foreground="White" FontSize="12">Whisper Mode</CheckBox>
<TextBox x:Name="chatTxtBoxType" Height="39" Margin="10,0,313,9" VerticalAlignment="Bottom" />
<Button x:Name="chatButtonSend" Click="chatButtonSend_Click" Height="39" Margin="0,0,167,9" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="136">Send</Button>
<Button x:Name="chatButtonDisconnect" Click="chatButtonDisconnect_Click" Height="39" HorizontalAlignment="Right" Margin="0,0,10,9" VerticalAlignment="Bottom" Width="139">Disconnect</Button>
<Image x:Name="chatCurrentImage" HorizontalAlignment="Left" Margin="10,0,0,0" Stretch="Fill" Width="60" Height="70" VerticalAlignment="Top" />
<Label x:Name="chatLabelCurrentUName" Height="23" HorizontalAlignment="Left" Margin="87,10,0,0" VerticalAlignment="Top" Width="85" Foreground="White"></Label>
<Label x:Name="chatLabelCurrentStatus" Height="23" Margin="87,37,0,0" VerticalAlignment="Top" Foreground="Green" HorizontalAlignment="Left" Width="85"></Label>
<Label x:Name="chatLabelWritingMsg" Height="30" Margin="10,0,167,49" VerticalAlignment="Bottom" Foreground="Gray"></Label>
<Button x:Name="chatButtonSendFile" Click="chatButtonSendFile_Click" Background="Transparent" Height="23" Margin="270,10,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="105">Send File</Button>
<Label x:Name="chatLabelSendFileStatus" Height="28" Margin="270,32,316,0" VerticalAlignment="Top"></Label>
<Button x:Name="chatButtonOpenReceived" Click="chatButtonOpenReceived_Click" Background="Transparent" Height="23" Margin="382,10,313,0" VerticalAlignment="Top">Open Received Files</Button>
</Grid>
</Window>
The reason why this is occurring is because you are explicitly setting Margin properties on your elements.
Margin="277,232,313,0"
These margins are relative to the parent Grid. So the code above says:
Place the element 277 from the left, 232 from the top, 313 from the right and 0 from the bottom.
Therefore, when the Grid gets resized, these margins will be recalculated, and thus, screw up the positioning of the elements.
To combat this, you should look into using relative positioning. There is already an answer to this question here.
I found a simple solution.when I select an object like button or ..., a small link icon(similar to above Hyperlink icon) appears on each edge of the object (or on grids around it). by clicking on these icons, you can change them into Unlinked or Linked situation. now when you want to resize the main window, you should do the following:
1. move all the objects to the new place you want in the form.
2. select the objects one after another and click on link icon at the side that you want to decrease the main window's width or height from that side.for example, if you want to decrease the width/height of the window from right/down side, you should unlink every "right/down" side link icon.
3. resize the width or height of main window by dragging its edge according to the 2nd step example. you can see that resizing window does not affect other objects at all.
I have a WrapPanel and three buttons on it. the third button in WrapPanel is in second line.
I want to find the real psotion of button4.
<WrapPanel Height="100" Width="200">
<Button Content="Button" Height="23" Name="button2" Width="75" />
<Button Content="Button" Height="23" Name="button3" Width="75" />
<Button Content="Button" Height="23" Name="button4" Width="75" />
</WrapPanel>
I use this code but it is 0 because margin is 0.
int top = button4.Margin.Top //I want in this case top become 23 but it is 0.
To find the button position in the WrapPanel, you should use TransformToAncestor (msnd)
Point currentPoint = button4.TransformToAncestor(myWrapPanel).Transform(new Point(0, 0));
Where myWrapPanel is your WrapPanel.
I am using WPF and DevExpress. I want to add two buttons in dock panel. As I added 2nd button it said: Content is set more than once. Why is it so ? Did I made any mistake or is dock panel doesn't allow this.
<dxdo:DockLayoutManager Margin="0,-3,-156,0" HorizontalAlignment="Right" VerticalAlignment="Top" Background="White" Height="243" Width="109">
<dxdo:LayoutGroup Caption="LayoutRoot" Margin="0,0,0,172">
<dxdo:LayoutGroup Orientation="Vertical">
<dxdo:LayoutPanel x:Name="Panel1" ShowCloseButton="False" ShowMaximizeButton="False" ShowPinButton="False" ShowRestoreButton="False" ShowControlBox="False" AllowDock="False" AllowDrag="False" AllowFloat="False" AllowHide="False" AllowClose="False" AllowActivate="False" AllowMinimize="False" AllowMaximize="False">
<Button x:Name="ToggleButton1" Content="New" Click="ToggleButton_Click" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Button Content="Delete Focused Row"
Click="DeleteButton_Click33"
Grid.Column="1" />
</dxdo:LayoutPanel>
<dxdo:LayoutPanel x:Name="Panel2" ShowCloseButton="False" ShowMaximizeButton="False" ShowPinButton="False" ShowRestoreButton="False" ShowControlBox="False" AllowDock="False" AllowDrag="False" AllowFloat="False" AllowHide="False" AllowClose="False" AllowActivate="False" AllowMinimize="False" AllowMaximize="False">
<Button x:Name="ToggleButton2" Content="Close" Click="ToggleButton2_Click" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</dxdo:LayoutPanel>
</dxdo:LayoutGroup>
</dxdo:LayoutGroup>
</dxdo:DockLayoutManager>
You can place more than one control into the LayoutPanel.Content property. Just wrap your buttons into panel:
<dxdo:LayoutPanel ... >
<StackPanel Orientation="Vertical">
<Button x:Name="button1" ... />
<Button x:Name="button2" ... />
</StackPanel>
</dxdo:LayoutPanel>
Related example: How to build a layout of controls within LayoutPanels