If you programmatically select an item on a WPF datagrid, you may need to scroll to make sure that item is visible. One line of code will take care of that for you.
myGrid.ScrollIntoView(myGrid.SelectedItem);
That should do it.
Happy Coding!
All about GUI's, WPF and Silverlight
If you programmatically select an item on a WPF datagrid, you may need to scroll to make sure that item is visible. One line of code will take care of that for you.
myGrid.ScrollIntoView(myGrid.SelectedItem);
That should do it.
Happy Coding!
This is a very common mistake and I found myself committing it recently.
Sometimes you will find yourself wanting to create a datagrid column that contains a checkbox. The simplest way of doing this is by defining a DataGridCheckBoxColumn.
<DataGridCheckBoxColumn Binding="{Binding IsSelected}"/>
There are some situations when you might want to use a DataGridTemplateColumn instead because you want to tweak some visuals or add other controls inside the cell.
<DataGridTemplateColumn Width="30">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
When people do this, they sometimes find that the bindings don’t work properly. If they click on the checkbox, they notice that the property they are binding to (in this case IsSelected) does not update until you click somewhere else in the cell. Some people even miss this and just think the binding does not work at all. I ran across a case where a developer had two datagrid columns side by side, one was the DataGridCheckBoxColumn and the other one was a DataGridTemplateColumn like the ones above. The developer was convinced that something was going on because the binding was working for one and not the other.
If you run into this, don’t worry. The binding is working. It’s just not updating the bound property at the correct time. To fix this, simply make the following tweak to the binding statement. This should now cause the property to change immediately once the checkbox is checked (once the IsChecked property changes).
<DataGridTemplateColumn Width="30">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
That’s it. Hope this helps.
Happy Coding!
<Button Content="Button" ToolTip="This is a tooltip"/>
<Button Content="Button" ToolTip="This is a tooltip" ToolTipService.HasDropShadow="False"> </Button>
BetweenShowDelay | This value specifies how much time (in milliseconds) the user has to move from the control that is displaying the tooltip to another one without having to wait for it’s InitialShowDelay. Essentially, if I set this value to X, once I see the tooltip on my control, I have X milliseconds to hover over another control that has a tooltip and that other tooltip will show up immediately. |
HasDropShadow | Specifies if the tooltip has a dropshadow. |
HorizontalOffset | Offset from the left of the area that is specifies for the tooltip. |
InitialShowDelay | How long it takes for the tooltip to open (in milliseconds). |
IsEnabled | Determines whether the tooltip gets displayed. |
IsOpen | Gets whether the tooltip is open. |
Placement | Sets the orientation of the tooltip when it opens |
PlacementRectangle | Gets or sets the rectangle area in which the tooltip is displayed |
ShowDuration | Specifies the amount of time the tooltip is displayed |
ShowOnDisabled | Specifies if the tooltip is displayed when the parent object is disabled |
ToolTip | Gets or sets the content of the tooltip |
VerticalOffset | Offset from the top of the area that is specifies for the tooltip. |
<Button Content="Button"> <ToolTipService.ToolTip> <Rectangle Fill="Red" Height="20" Width="20"/> </ToolTipService.ToolTip> </Button>
<Grid> <StackPanel> <Label Content="My First WPF App"/> <Button Content="Hello World"/> <TextBox/> </StackPanel> </Grid>
Grid g = new Grid(); StackPanel s = new StackPanel(); Label l = new Label(); l.Content = "My First WPF App"; Button b = new Button(); b.Content = "Hello World"; TextBox t = new TextBox(); //populate stack panel s.Children.Add(l); s.Children.Add(b); s.Children.Add(t); //add stackpanel to grid g.Children.Add(s); //set application content this.Content = g;
<Button Height="50" Width="100"> <StackPanel Orientation="Horizontal"> <Label Content="Click"/> <Image Source="click_here.png"/> </StackPanel> </Button>
<Grid> <Button/> </Grid>
The logical tree for this code is represented as
The visual tree refers to the various visual elements that make up a logical element. The logical tree just for the button above is represented as:
<Style TargetType="{x:Type Label}"> <Style.Setters> <Setter Property="Foreground" Value="Red"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="FontSize" Value="40"/> </Style.Setters> </Style> <Style TargetType="{x:Type Label}" x:Key="BigBlueStyle"> <Style.Setters> <Setter Property="Foreground" Value="Blue"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="FontSize" Value="40"/> </Style.Setters> </Style> <Style TargetType="{x:Type Label}" x:Key="LittleGreenStyle"> <Style.Setters> <Setter Property="Foreground" Value="Green"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="FontSize" Value="10"/> </Style.Setters> </Style>
<StackPanel> <Label Content="First Label"/> <Label Content="Second Label" Style="{DynamicResource BigBlueStyle}"/> <Label Content="Third Label" Style="{DynamicResource LittleGreenStyle}"/> <Label Content="Fourth Label" Foreground="Purple"/> </StackPanel>
<Button Height="100" Width="200"> <Button.Template> <ControlTemplate> <Grid Background="Green"> <StackPanel VerticalAlignment="Center"> <TextBlock Text="Click Me"/> <Rectangle Fill="Black" Height="30" /> <ComboBox> <ComboBox.Items> <ComboBoxItem Content="Item One"/> <ComboBoxItem Content="Item Two"/> <ComboBoxItem Content="Item Three"/> </ComboBox.Items> </ComboBox> </StackPanel> </Grid> </ControlTemplate> </Button.Template> </Button>
If the expression’s value does not match any of the constant values in the switch statement, the control flow is transferred to the statement following the default keyword.switch (expression) { case constant1: statement1; break; case constant2: statement2; break; case constant3: statement3; break; default: default statement; break; }
if (expression1) { statement1; } else if (expression2) { statement2; } else if (expression3) { statement3; } else { default statement; }
switch (expression) { case constant1: case constant2: statement1; break; case constant3: statement3; break; default: default statement; break; }