Bounded Checkbox not firing event - wpf

I have a check box where the property ischecked is bounded to another checkbox. In the resources block i've set a style that sets the event triger for a checkbox. Here's the code:
<Style x:Key="cbClickEvent" TargetType="{x:Type CheckBox}">
<EventSetter Event="Click" Handler="CheckBox_Click"/>
</Style>
<StackPanel Grid.Column="0" Grid.Row="1">
<CheckBox x:Name="cbAll" Content=" All" VerticalAlignment="Center"/>
<CheckBox Content=" MyCB" VerticalAlignment="Center" IsChecked="{Binding ElementName=cbAll, Path=IsChecked, Mode=OneWay}" Style="{StaticResource cbClickEvent}"/>
</StackPanel>
When i click the checkbox MyCB the event is triggered, but not when i clicked the checkbox All. What am i missing?

This is because when you click on the "All" checkbox the value gets transfered to the "MyCB" checkbox via binding. But in the style you subscribe to the Clickevent which obviously is not firing in this case because you don't actually click on the "MyCB" checkbox (you click on "All").
What you can do is, instead of subscribing to the Click event, you can subscribe to the Checked and Unchecked events:
<EventSetter Event="Checked" Handler="CheckBox_Checked"/>
<EventSetter Event="Unchecked" Handler="CheckBox_Unchecked"/>

2 possible solitions:
1) Add the
Style="{StaticResource cbClickEvent}"
To the cbAll CheckBox
2) Remove that line from the MyCB CheckBox and remove the x:Key property from the style. It will look like this:
<Window.Resources>
<Style TargetType="{x:Type CheckBox}">
<EventSetter Event="Click" Handler="CheckBox_Click"/>
</Style>
</Window.Resources>
<StackPanel Grid.Column="0" Grid.Row="1">
<CheckBox x:Name="cbAll"
Content=" All"
VerticalAlignment="Center" />
<CheckBox Content=" MyCB"
VerticalAlignment="Center"
IsChecked="{Binding ElementName=cbAll, Path=IsChecked, Mode=OneWay}" />
</StackPanel>

Related

How to have multi selection and checkbox work together in telerik WPF

I have check boxs inside my radchombobox and if I select multiple names it shows in the text field but noting happens till I click the check box then the radcombobox closes after one check. How to I combine both actions so if I select the names or check the check boxes it work. Also how do I get the combobox not to close after I check one item. New to telerik WPF thnka you
<Grid>
<telerik:RadComboBox x:Name="radComboBox"
VerticalAlignment="Center"
Margin="0 50 0 0"
Grid.Column="1"
Grid.Row="1"
ItemsSource="{Binding Items}"
AllowMultipleSelection="True">
<telerik:RadComboBox.ItemContainerStyle>
<Style TargetType="{x:Type telerik:RadComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPlanel Orientation "Horizontal">
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"
VerticalAlignment="Center"
Width="5"/>
<TextBlock Text="{Binding Name}"/>
<StackPlanel>
<ControlTemplate>
<Setter.Value>
<Setter>
<Style>
<telerik:RadComboBox.ItemContainerStyle>
</telerik:RadComboBox>
<Grid>
"Name" is a string and "Items" is an ObservableCollection

List box modify datatemplate of items at runtime from code behind

i have this listbox
<ListBox x:Name="lbsample" ItemsSource="{Binding CurrentSettings}" SelectionMode="Extended">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" >
<Setter Property="Focusable" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<GroupBox x:Name="gb0" Margin="0" Background="Transparent">
<GroupBox.Header>
<CheckBox x:Name="chk0" Content="xxx"
IsChecked="{Binding Path=DataContext.IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" />
</GroupBox.Header>
<!---->
<StackPanel Orientation="Horizontal" IsEnabled="{Binding Path=IsEnabled}" >
<TextBlock x:Name="lbltxt" Text="yyy"
HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5,5,5,10"/>
</StackPanel>
</GroupBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
when mask is loaded i want to change the text 'xxx' and 'yyy' progmatically from code behind
how can i do that? i cannot reach the properties because listbox is bound and has itemsource
i know is not best practice.. but i do not have easy alternatives..
Thanks!
if you need something view-specific, add Loaded event handler:
<CheckBox x:Name="chk0" Content="xxx" Loaded="CheckBoxLoaded" ...>
then in code-behind get CheckBox from sender argument and change properties:
private void CheckBoxLoaded(object sender, RoutedEventArgs e)
{
var chk = sender as CheckBox;
}
however you can declare special property in item class and simple bind CheckBox:
<CheckBox x:Name="chk0" Content="{Binding PropertyX}" ...>
same approaches should work for TextBlock.

Tooltip is not visible on the disabled checkbox wpf

Tooltip is not visible on disabled checkbox even after setting ToolTipService.ShowOnDisabled="True".
<CheckBox Grid.Column="0" HorizontalAlignment="Center" Visibility="{Binding Converter={StaticResource TaskCompletionVisbilityConverter},ConverterParameter='chkbox'}"
Height="16" Width="16" Foreground="{Binding Converter={StaticResource TaskColorConverter}}"
ToolTipService.ShowOnDisabled="True" ToolTipService.IsEnabled="True"
ToolTip="Check To complete Task"
IsEnabled="{Binding State, Converter={StaticResource EnableDisableConverter},
ConverterParameter='checkbox',Mode=TwoWay}" Margin="37,5,0,0">
Your code works for me. ToolTipService.ShowOnDisabled="True" should be what you need.
Please check that you haven't set IsHitTestVisible to false in any Style/ControlTemplate associated with the checkbox.
Setting IsHitTestVisible to false means that it will ignore any mouse events associated with the control and so you won't get the tooltip.
ToolTipService.ShowOnDisabled="True" was not working for me. Below works :
<DataGridCheckBoxColumn.CellStyle>
<Style>
<Setter Property="ToolTipService.ShowOnDisabled" Value="True" />
</Style>
</DataGridCheckBoxColumn.CellStyle>

Panel.ZIndex property in a treeviewitem in wpf

I Wish the treeviewitem to overlap the other items when i mouse hover it.
To do this i made the parent element (in my case its Border) within the HierarchicalDataTemplate to have the ZIndex as 0 and changed this value to 1 when the user hovers the mouse in the HierarchicalDataTemplate.Triggers section
<HierarchicalDataTemplate DataType="{x:Type d:MyClass}">
<Border Name="brd" CornerRadius="5" BorderThickness="1" Padding="3" Margin="0,0,60,0" Panel.ZIndex="0" >
<StackPanel Orientation="Horizontal" Margin="0,0,0,0" >
<Image Source="../Images/icon.jpg" Height="30"></Image>
<TextBlock TextAlignment="Center" Text="{Binding Text}"
Margin="3,0,10,0" >
</TextBlock>
<Image Margin="0,0,3,0"
Source="../Images/Img1.jpg" Height="30" />
<Image Margin="0,0,0,0"
Source="../Images/Img2" Height="30"/>
</StackPanel>
</Border>
<HierarchicalDataTemplate.Triggers>
<Trigger SourceName="brd" Property="IsMouseOver" Value="True">
<Setter TargetName="brd" Property="Panel.ZIndex" Value="1"></Setter>
</Trigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
The whole idea to implement this was:
Whenever the user hovers the mouse over a treeviewitem, the item should overlap the other controls and should be completely visible. Example: if the item is a long text, then the user should not be forced to use the scroll bar, rather if he just points it the item should be overlapping the other controls to display the complete item.
But i couldn't achieve this using the above triggers.
Please help me doing this.
Did you try using a ToolTip? I didn't try it myself but after seeing this I'm convinced that it's possible to define a DataTemplate which can be used at this property so the item is shown the way you like.
I hope this helps.
Regards

XAML/WPF: How do I bind a command to a comboboxitem

I want to bind a command to a comboboxitem much like a typical command (either application or custom) is bound to a button. I can't seem to find an example.
EDIT:
The ComboBoxItem exists as part of a ContentControl that also contains a button. The button has an associated command that works well. How can I bind a command, in a similar way, to the ComboBoxItem?
<!-- Line -->
<Button x:Name="Line"
Style="{DynamicResource Button_Title}"
Grid.Row="1"
Grid.RowSpan="3"
Grid.Column="0"
Content="Line"
Command="{x:Static local:Ribbon_AC.Custom_RoutedUICommand}"
CommandParameter="Line"
Tag="{DynamicResource Line_32}"/>
<!-- Arc -->
<ContentControl x:Name="ArcSplit"
Template="{DynamicResource Control_SplitSmall}"
Tag="{DynamicResource Arc_20}"
Grid.Row="1"
Grid.Column="1">
<ComboBox Name="ComboBox_Arc"
Style="{DynamicResource ComboBox_Small}"
Width="{DynamicResource Width_DropDown}">
<!-- Arc_0 -->
<ComboBoxItem x:Name="Arc_0"
Style="{DynamicResource ComboBoxItem_Large}"
Tag="{DynamicResource Arc0_32}"
Content="Arc 0">
</ComboBoxItem>
<ComboBox>
<ComboBox.Resources>
<Style TargetType="ComboBoxItem">
<EventSetter Event="Foo" Handler="Bar"/>
</Style>
</ComboBox.Resources>
</ComboBox>
As one of the options you can consider putting a Button into the ComboBox's ItemTemplate and change the Button's template to make it look "not like a button at all".
But that is dirty. Maybe you can use Menu instead of ComboBox?

Resources