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.
Related
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)
{
}
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>
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.
I'd like to have 4 buttons one in each corner of a WPF/Silverlight window. But I want the stuff in the grid/window to be "behind" the buttons, as if they float on top.
<Grid x:Name="ButtonRoot">
<Button VerticalAlignment="Top" HorizontalAlignment="Left"
Name="bTopLeft" />
<Button VerticalAlignment="Top" HorizontalAlignment="Right"
Name="bTopRight" />
<Button VerticalAlignment="Bottom" HorizontalAlignment="Left"
Name="bBottomLeft" />
<Button VerticalAlignment="Bottom" HorizontalAlignment="Right"
Name="bBottomRight" />
<!-- Other junk here -->
</Grid>
The problem is, the buttons will not be "over" things, as the things will "wrap" around the buttons. How do I achieve this effect?
Diagram of how I want it http://dl.compdj.com/images/stackButton.jpg
Use two grids, remember whatever is farther down the file will be on top:
<Grid>
<Grid Background="Green"><!-- put stuff here --></Grid>
<Grid><!-- this goes on top -->
<Button Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Content="Button" /><!-- top left button -->
<!-- etc -->
</Grid>
</Grid>
this should solve your problem
#bTopLeft {
position: absolute;
top: 0;
left: 0;
z-index: 1200;
}