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>
Related
I have a multiselect ComboBox with the following template:
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox
VerticalAlignment="Center"
Checked="checkBox_Checked"
ClickMode="Press"
Unchecked="checkBox_Unchecked" />
<TextBlock Text="{Binding Position}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
My question is, if the checkBox is checked, how could I get the value from the TextBlock (what should be the code in checkBox_Checked? I will be needing it for further use.
There shouldn't be any Checked or Unchecked event handler.
Instead, the view model (that exposes the Position property) should expose another property, to which you bind the IsChecked property of the CheckBox:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Checked}" />
<TextBlock Text="{Binding Position}" />
</StackPanel>
</DataTemplate>
In the setter of the Checked property you can access the Position property.
Also be aware that the TextBlock in the ItemTemplate seems to be redundant:
<DataTemplate>
<CheckBox IsChecked="{Binding Checked}"
Content="{Binding Position}" />
</DataTemplate>
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.
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>
I want to show a duplicate of a control which behaves same as source.In below mentioned code I have textbox1 that I want to show again.So I created one more text and binded its text with textbox1's text.If I change the text in textbox1 ,text changes in other tetxbox also.
But my problem is validation template is not getting applied on other control.How can I do that?
<TabItem Width="100" Height="50" Header="Tab1">
<AdornerDecorator>
<StackPanel>
<Label> First</Label>
<TextBox x:Name="TextBox1"
Margin="20" Height="50" Width="150"
Style="{StaticResource S_ErrorTemplate}"
Text="{Binding TestValue,
ValidatesOnDataErrors=True,
NotifyOnValidationError=True,
UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Margin="20" Height="50" Width="150"
Style="{StaticResource S_ErrorTemplate}"
Text="{Binding ElementName=TextBox1, Path=Text,
ValidatesOnDataErrors=True,
NotifyOnValidationError=True,
UpdateSourceTrigger=PropertyChanged}"
Template="{Binding ElementName=TextBox1,Path=Template}"/>
</StackPanel>
</AdornerDecorator>
</TabItem>
output is
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}" />