Binding combobox IsEnabled to sibling CheckBox control's state - c#

I have this code. What I'm trying to make is that when you dial checkbox, the combobox is enabled and if unmarked, it locks.
<DockPanel Margin="0,0,10,0">
<CheckBox Margin="5,5,0,5" HorizontalAlignment="Center"
VerticalAlignment="Center" IsChecked="True" Content="Cliente:" FontSize="15"/>
<ComboBox Width="150"
ItemsSource="{Binding Clients}"
DisplayMemberPath="FullDescription"
SelectedItem="{Binding SelectedClient}"
IsEnabled="{Binding IsChecked, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CheckBox}}}"/>
</DockPanel>

I can't post comments yet so I'll post here.
Have you looked at these other posts:
Disable text box when Check box is Unchecked during run time in C#
How to bind inverse boolean properties in WPF?
EDIT:
Try this:
<DockPanel Margin="0,0,10,0">
<CheckBox x:Name="chkEnableBackup" Margin="5,5,0,5" HorizontalAlignment="Center"
VerticalAlignment="Center" IsChecked="True" Content="Cliente:" FontSize="15"/>
<ComboBox Width="150"
ItemsSource="{Binding Clients}"
DisplayMemberPath="FullDescription"
SelectedItem="{Binding SelectedClient}"
IsEnabled="{Binding ElementName=chkEnableBackup, Path=IsChecked}"/>
</DockPanel>

Related

WPF bind list box Selected item into textbox

I'm trying to put any selected item's name in my list box in the textbox next to it. But I've got trouble doing so.
Here's a little bit of my code:
<DockPanel Margin="10">
<StackPanel Margin="5" DockPanel.Dock="Left">
<ListBox Name="lbNames" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True"/>
</StackPanel>
<StackPanel Margin="5" DockPanel.Dock="Right">
<TextBox Name="txtName" Width="auto" Text="{Binding ElementName=lbNames,Path=SelectedItem}"/>
<WrapPanel Margin="0,5" Orientation="Horizontal">
<Button Margin="2,0" Name="btnAdd" Click="btnAdd_Click" Content="Add"/>
<Button Margin="2,0" Name="btnEdit" Click="btnChange_Click" Content="Edit"/>
<Button Margin="2,0" Name="btnDelete" Click="btnDelete_Click" Content="Delete"/>
</WrapPanel>
</StackPanel>
</DockPanel>
Because you are showing Text="{Binding ElementName=lbNames,Path=SelectedItem}"and your selected item is an user object.
Instead of this you can use something like that.
First add SelectedValuePath="Name" into your listbox.
Then use Text="{Binding ElementName=lbNames,Path=SelectedValue}"
<DockPanel Margin="10">
<StackPanel Margin="5" DockPanel.Dock="Left">
<ListBox Name="lbNames" DisplayMemberPath="Name" SelectedValuePath="Name" IsSynchronizedWithCurrentItem="True"/>
</StackPanel>
<StackPanel Margin="5" DockPanel.Dock="Right">
<TextBox Name="txtName" Width="auto" Text="{Binding ElementName=lbNames,Path=SelectedValue}"/>
<WrapPanel Margin="0,5" Orientation="Horizontal">
<Button Margin="2,0" Name="btnAdd" Click="btnAdd_Click" Content="Add"/>
<Button Margin="2,0" Name="btnEdit" Click="btnChange_Click" Content="Edit"/>
<Button Margin="2,0" Name="btnDelete" Click="btnDelete_Click" Content="Delete"/>
</WrapPanel>
</StackPanel>
</DockPanel>
Or simply you can use it too Text="{Binding ElementName=lbNames,Path=SelectedItem.Name}"

Why is my ComboBox ToolTip not in synch with the ComboBoxItem ToolTip all the time?

I am trying to update the Combo ToolTip at the same time as the ComboItems.
<ComboBox x:Name="comboMeetingWeek" ItemsSource="{Binding Meetings}"
SelectedItem="{Binding Meeting, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.ToolTip>
<ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}"
Content="{Binding Path=SelectedItem.ToolTipForSpecialEvent}">
</ToolTip>
</ComboBox.ToolTip>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" ToolTip="{Binding ToolTipForSpecialEvent}">
<Image Source="Images/Bell.png" Margin="0,0,5,0"
Visibility="{Binding DisplayBellImage, Converter={StaticResource BoolToHiddenConverter}}" Stretch="None"/>
<TextBlock Text="{Binding DateMeetingAsText}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The ComboBoxItems will always be correct. The but the ComboBox ToolTip won't.
OK, I found the solution. I had to do it like this:
<ComboBox x:Name="comboMeetingWeek" ItemsSource="{Binding Meetings}"
SelectedItem="{Binding Meeting, UpdateSourceTrigger=PropertyChanged}"
ToolTip="{Binding Meeting.ToolTipForSpecialEvent}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" ToolTip="{Binding ToolTipForSpecialEvent}">
<Image Source="Images/Bell.png" Margin="0,0,5,0"
Visibility="{Binding DisplayBellImage, Converter={StaticResource BoolToHiddenConverter}}" Stretch="None"/>
<TextBlock Text="{Binding DateMeetingAsText}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Less code ... even better .. :) But now is always works right.
This link helped.

Bind to command from within a treeview

I'm building a treeview with HierarchicalDataTemplates and would like to bind the nodes to a command from my MainViewModel. I guess there is some conflict with the scopes, since the binding works if I e.g. use a button and define it outside of the treeview. If I define it inside, however, it does not work.
I've searched through Stackoverflow and found several solutions but none that worked for me. Jehof e.g. suggested here to use
<Button Command="{Binding DataContext.Command,
RelativeSource={RelativeSource AncestorLevel=2, AncestorType=TreeViewItem}}"
CommandParameter="{Binding}" />
but that did not work. Thank you for any suggestions!
<TreeView ItemsSource="{Binding _questions}" Grid.Row="0" Margin="10" BorderThickness="1">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Question}" ItemsSource="{Binding Converter={StaticResource QuestionConverter}}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:MainOption}" ItemsSource="{Binding MainOptions}">
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding Path=Name}" />
/////////////////////////////////////
<Button Command="{Binding ThisIsMyCommand}" Content="Click Me"/>
/////////////////////////////////////
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
Is your TreeView-Control inside a Window or UserControl?
If you are inside a Window:
<Button Command="{Binding DataContext.Command, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" CommandParameter="{Binding}" />
and for UserControl
<Button Command="{Binding DataContext.Command, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" CommandParameter="{Binding}" />

Obtain the value from textbox inside of a DataTemplate (WPF)

I´ve just created a datatemplate for a ListBox like that:
<ListBox Height="150" MinHeight="100" HorizontalAlignment="Left" Name="myListBox"
VerticalAlignment="Top" Width="290"
ItemsSource="{Binding}" SelectionMode="Multiple" Margin="0,18,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<StackPanel Orientation="Horizontal">
<CheckBox Name="cbLista" Width="100" Content="{Binding Path=Nom_estudio}" IsChecked="{Binding IsChecked, Mode=TwoWay}"
Checked="cbLista_Checked" />
<TextBox Name="txbCantidad" Width="100" Margin="0,0,0,5" TextChanged="txbCantidad_TextChanged" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And now I can get every object selected in the checkbox, but how can I obtain the text property for the texbox asociated for every checkbox ?
Bind the "Text" property of the TextBox to some property (say MyTextProperty) on your data object. Then when you get the "SelectedItems" list, you just access this property.
ie:
<TextBox Text="{Binding MyTextProperty}" ... />
Create on more property in your class which has Nom_estudio and IsChecked properties. Then bind that property to TextBox.Text property
<StackPanel Orientation="Horizontal">
<CheckBox Name="cbLista" Width="100" Content="{Binding Path=Nom_estudio}" IsChecked="{Binding IsChecked, Mode=TwoWay}"
Checked="cbLista_Checked" />
<TextBox Name="txbCantidad" Text="{Binding MYTEXTPROPERTY}" Width="100" Margin="0,0,0,5" TextChanged="txbCantidad_TextChanged" />
</StackPanel>

Bind CheckBox from DataTemplate to TemplatedParent in ListBox

I have next code:
<ListBox Grid.Column="1" Grid.Row="4" Grid.RowSpan="2" Margin="0,0,1,0" MinHeight="80" Name="lbThemes" SelectionMode="Multiple" IsEnabled="True">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<CheckBox x:Name="ThemeCheckbox" />
<TextBlock Text="{Binding Path=label, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to bind my checkbox in dataTemplate to the ListBoxItem IsSelected property. Any idea how can I do this?
P.S. I have use Multiple Selesction mode
Try the following
<CheckBox x:Name="ThemeCheckbox" IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" />

Categories