WPF: Element Justification - wpf

Is there a way to justify three or more elements in WPF along the window size,
in such a way that it would look like this:
[button] [button] [button] [button]
________________________________________________________
I tried searching for ways using dockpanel/stackpanel, but they only seem to use two elements (for attaching one to the left, and one to the right, for example.)

here is a sample using UniformGrid
<UniformGrid Columns="4">
<Button Content="1" />
<Button Content="2" />
<Button Content="3" />
<Button Content="4" />
</UniformGrid>
if you do not want to fix the columns to 4 then fix the rows to 1 and all the buttons will be uniformly spaced
<UniformGrid Rows="1">
<Button Content="1" />
<Button Content="2" />
<Button Content="3" />
<Button Content="4" />
<Button Content="5" />
<Button Content="6" />
</UniformGrid>

Related

A value of type toolbar cannot be added to a collection or dictionary of type Collection

I get the error described in the title using Toolbar(s) in a ToolBarTray like the following:
<ToolBarTray DockPanel.Dock="Top">
<ToolBar>
<Button Command="New" Content="New" />
<Button Command="Open" Content="Open" />
<Button Command="Save" Content="Save" />
</ToolBar>
<ToolBar>
<Button Command="Cut" Content="Cut" />
<Button Command="Copy" Content="Copy" />
<Button Command="Paste" Content="Paste" />
</ToolBar>
</ToolBarTray>
I can compile the program, it works perfectly, but I continue to get the error in Visual Studio.
Looks like I'm missing something, but I have no clue of what it is...
Any idea? Thanks in advance!
Edit Problem magically disappeared. The only thing I can say is that this morning I did a reboot (I'm not sure if I did reboots while having the error: I often hybernate my machine).

Trigger Silverlight button mouseover event for two buttons simultaneously

I have two sets of buttons in two wrappanles that are related to each other. When I move the mouse over a button in one wrappanel, I want its related button to light up at the same time i.e. firing both buttons' mouseover events. So in my code exampes, I when moving over button with label "1.Block", it and its matching button in the other row, "1.Sqaure", must both display their mouseover styles/animations.
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition Height="60" />
</Grid.RowDefinitions>
<toolkit:WrapPanel Grid.Column="0" Grid.Row="0">
<Button Content="1. Block" Width="100" />
<Button Content="2. Block" Width="100" />
<Button Content="3. Block" Width="100" />
<Button Content="4. Block" Width="100" />
<Button Content="5. Block" Width="100" />
<Button Content="6. Block" Width="100" />
<Button Content="7. Block" Width="100" />
<Button Content="8. Block" Width="100" />
</toolkit:WrapPanel>
<toolkit:WrapPanel Grid.Column="0" Grid.Row="1">
<Button Content="1. Square" Width="100" />
<Button Content="2. Square" Width="100" />
<Button Content="3. Square" Width="100" />
<Button Content="4. Square" Width="100" />
<Button Content="5. Square" Width="100" />
<Button Content="6. Square" Width="100" />
<Button Content="7. Square" Width="100" />
<Button Content="8. Square" Width="100" />
</toolkit:WrapPanel>
</Grid>
I have no idea where to start on this? Trigger perhaps? Any suggestions will be appreciated.
I think the easiest way to do this is to use VisualStateManager. Following example may be a little rough but should be sufficient overall.
So what we need is to link MouseEnter and MouseLeave events for each pair of buttons in two lists. I don`t have WrapPanel so I used StackPanels instead, but this should not matter if we can get all child buttons from them. Say we have this:
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal">
<StackPanel Name="panel1">
<Button Content="1. Block" Width="100" />
<Button Content="2. Block" Width="100" />
</StackPanel>
<StackPanel Name="panel2">
<Button Content="1. Square" Width="100" />
<Button Content="2. Square" Width="100" />
</StackPanel>
</StackPanel>
Using panels by name we take all their children (this can be done in constuctor for example) and iterate through them assigning same event handlers that move both buttons to required state:
var panel1Buttons = panel1.Children.OfType<Button>().ToList();
var panel2Buttons = panel2.Children.OfType<Button>().ToList();
for (int i = 0; i < panel1Buttons.Count(); i++)
{
var button1 = panel1Buttons[i];
var button2 = panel2Buttons[i];
//assign same mouse enter event handler
MouseEventHandler enterHandler = (s, e) =>
{
VisualStateManager.GoToState(button1, "MouseOver", true);
VisualStateManager.GoToState(button2, "MouseOver", true);
};
button1.MouseEnter += enterHandler;
button2.MouseEnter += enterHandler;
//assign same mouse leave handler
MouseEventHandler leaveHandler = (s, e) =>
{
VisualStateManager.GoToState(button1, "Normal", true);
VisualStateManager.GoToState(button2, "Normal", true);
};
button1.MouseLeave += leaveHandler;
button2.MouseLeave += leaveHandler;
}
So now when mouse enters any of paired buttons they both go to MouseOver state and return to Normal on when mouse leaves.
This us purely UI related functionality so even if you follow MVVM it`s ok to place this in code behind.

Assign controls to a property in XAML

Hej all,
I wonder if it's possible to assign more than one control to a property in XAML.
Say I have 2 controls in my XAML:
<Button x:Name="_Btn1 Content="Button 1" />
<Button x:Name="_Btn2 Content="Button 2" />
<local:MyControl x:Name="_MyCtrl" Controls="{what goes here?}" />
Or should I declare my control as a container control and put all controls inside it, like so:
<local:MyControl x:Name="_MyCtrl">
<Button x:Name="_Btn1 Content="Button 1" />
<Button x:Name="_Btn2 Content="Button 2" />
</local:MyControl>
Thnx in advance!
Grtz,
Dwi
You can do that if you make MyControl inherit ItemsControl.
Then this
<local:MyControl x:Name="_MyCtrl">
<Button x:Name="_Btn1 Content="Button 1" />
<Button x:Name="_Btn2 Content="Button 2" />
</local:MyControl>
will work and this
<local:MyControl x:Name="_MyCtrl" ItemsSource="ViewModelorControlCollection" />
will work too.
The ViewModelorControlCollection can be defined in your ViewModel or as a static resource in your xaml.
It really depends what you're gonna do. Usually you'd simply define a DataTemplate instead.
<local:MyControl x:Name="_MyCtrl">
<local:MyControl.ControlsTemplate>
<DataTemplate>
<StackPanel>
<Button x:Name="btn1" Content="Button 1" />
<Button x:Name="btn2" Content="Button 2" />
</StackPanel>
</DataTemplate>
</local:MyControl.ControlsTemplate>
</local:MyControl>
You definitely need some kind of container. This can be a simple list or a specialized collection. Whatever you use in your control.
<local:MyControl>
<local:MyControl.Controls>
<ControlCollection> <!-- or whatever -->
<Button/>
<Button/>
</ControlCollection>
</local:MyControl.Controls>
</local:MyControl>
If you use a Panel or similar classes, you don't need to specify the ControlCollection explicitly. You then can define the controls like with e.g. the StackPanel.

wpf layout-system problem

I'm not too familiar with wpf layout-system. so i'm ready to start and understanding that. at the first of road i have a problem with wpf. so according to below markup, i have 4 button which when we run project everything is true.
<Window ... WindowStartupLocation="CenterOwner" SizeToContent="WidthAndHeight" Name="wMessage" ShowInTaskbar="False" ResizeMode="NoResize" WindowStyle="SingleBorderWindow">
<WrapPanel Orientation="Horizontal">
<Button Content="Button 1" Margin="10" />
<Button Content="Button 2" Margin="10" />
<Button Content="Button 3" Margin="10" />
<Button Content="Button 4" Margin="10" />
</WrapPanel>
at the right side and bottom of window something appears like a border which i don't know this comes from where!!!
alt text http://www.4freeimagehost.com/uploads/098a981c7a36.png
The problem i that you have given a Margin="10" for every button. By default it takes that single value for all the Four sides. If you don't want the Blank Space for Top and bottom but want them between the buttons then cahnge the XAML to following.
<Button Margin="12,0" Content="Button1"/>
<Button Margin="12,0" Content="Button1"/>
<Button Margin="12,0" Content="Button1"/>
<Button Margin="12,0" Content="Button1"/>
By default if you give only 2 values, first one is taken for both Left and Right and second one is taken for both Top and Bottom.

Align button content

I want the content of a wpf button to be left aligned
I tried the following but the text is still centered inside the button.
<Button >
<StackPanel HorizontalAlignment="Stretch">
<TextBlock HorizontalAlignment="Left" Text="Save"/>
</StackPanel>
</Button>
What do i do?
Found it, it's HorizontalContentAlignment.
:)
You don't need the StackPanel or the TextBlock. All you need is
<Button HorizontalContentAlignment="Left" Content="Save" />
You can align in two way, Horizontal and Vertical
<Button Content="Export" VerticalContentAlignment="Bottom" name="MyBtn1" />
or
<Button Content="Export" HorizontalContentAlignment="Center" name="MyBtn2" />
See the follow image to view the possibilities settings:

Resources