My problem is my checkbox content doesnt display underscores or the & symbol. I've read about the RecognizeAccessKey attribute, but i can't get it working. My listbox looks like this:
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding Channels}">
<ListBox.ItemTemplate>
<DataTemplate>
<ContentPresenter RecognizesAccessKey="False">
<CheckBox Content="{Binding Item}"
IsChecked="{Binding IsChecked}"
Width="149"
MinWidth="149" />
</ContentPresenter>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Can anybody tell me how to get this fixed, so my Checkbox's content will display underscores and & symbols?
thank you very much!
You could use TextBlock as CheckBox.Content, instead of binding it directly, and bind its Text property
<CheckBox IsChecked="{Binding IsChecked}" Width="149" MinWidth="149">
<CheckBox.Content>
<TextBlock Text="{Binding Item}"/>
</CheckBox.Content>
</CheckBox>
By default CheckBox will wrap TextBlock in AccessText when it displays Content
Related
I am collecting messages from a com port. When the message is received a time stamp is recorded. I then display the time stamp followed by the message. It looks like:
01/02/2022 13:14:15.123 00 48 B4 68 33
I want to add a checkbox that will show the timestamp when checked, and just the message when unchecked.
My XAML for displaying the timestamp and message:
<ListBox Grid.Row="1" x:Name="MessageList" Margin="10" ItemsSource="{Binding Path=SDIMessages}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding RecivedTime, StringFormat=dd-MM-yyyy HH:mm:ss.fff}"/>
<TextBlock Text="{Binding Message}" Margin="10,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My code for the checkbox
<CheckBox Grid.Row="0" VerticalAlignment="Bottom" Margin="10,0,0,0" IsChecked="{Binding ShowTimeStamp, Mode=TwoWay}" Content="Display Time Stamp"/>
Currently I have the binding for ShowTimeStamp working correctly. How do I hide the TimeRecived? I am still learning C#, XAML and MVVM. I believe I need to use a datatrigger somehow but how do I make the trigger from the checkbox affect a listbox?
you can give checkbox a name ("CheckTimeStamp") and bind TextBlocks Visibility directly to IsChecked property (without involving view model) using BooleanToVisibilityConverter:
<CheckBox Grid.Row="0" Name="CheckTimeStamp" VerticalAlignment="Bottom" Margin="10,0,0,0" IsChecked="{Binding ShowTimeStamp, Mode=TwoWay}" Content="Display Time Stamp"/>
<ListBox Grid.Row="1" x:Name="MessageList" Margin="10" ItemsSource="{Binding Path=SDIMessages}">
<ListBox.Resources>
<BooleanToVisibilityConverter x:Key="boolToVisibility"/>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding RecivedTime, StringFormat=dd-MM-yyyy HH:mm:ss.fff}"
Visibility="{Binding ElementName=CheckTimeStamp, Path=IsChecked, Converter={StaticResource boolToVisibility}}"/>
<TextBlock Text="{Binding Message}" Margin="10,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I've written a code example below:
<StackPanel>
<TextBlock>Appointments today</TextBlock>
<ItemsControl ItemsSource="{Binding Appointments}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<controls:CalendarMonthDayEventsItemsPanel OutsideViewportCount="..." />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border>
<TextBlock Text="{Binding Title}" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<StackPanel Orientation="Horizontal">
<TextBlock>+</TextBlock>
<TextBlock Text="{Binding ......}" /> <!-- Should show number of items in ItemsControl that are outside view, using CalendarMonthDayEventsItemsPanel.OutsideViewportCount -->
<TextBlock>more items</TextBlock>
</StackPanel>
</StackPanel>
I Created a custom Panel, the CalendarMonthDayEventsItemsPanel, and I use that panel as the itemspanel of the ItemsControl. In the panel's layout code, I determine how many items (panel childs) are outside the bounds of the panel. The property OutsideViewportCount contains the number of items that are outside of the visible bounds of the CalendarMonthDayEventsItemsPanel.
Now I want to show the value of OutsideViewportCount in a TextBlock that's below the ItemsControl.
This means I have to create some kind of binding, but everything I tried doesn't work.
Does someone have a solution or a better approach to achieve the same result?
Maybe you can make use of Tag property of ItemsPanel to relay the value of ItemsPanel as a workaround.
<ItemsControl x:Name="A"
ItemsSource="{Binding Appointments}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<local:CalendarMonthDayEventsItemsPanel OutsideViewportCount="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=Tag, Mode=OneWayToSource}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
...
</ItemsControl>
<StackPanel Orientation="Horizontal">
<TextBlock>+</TextBlock>
<TextBlock Text="{Binding ElementName=A, Path=Tag, Mode=OneWay}"/>
<TextBlock>more items</TextBlock>
</StackPanel>
I have ListBox :
<ListBox HorizontalAlignment="Left" Name="betOdds" VerticalAlignment="Top">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" HorizontalAlignment="Stretch"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Where OddsList is ObservableCollection<CheckBoxListItem>
I init this collection with different objects, that have Name like "TEST", "TEST_1", "TEST_2", "OVERFLOW" etc. (Some names contains _ symbol).
When I bind this collection to ItemsSource of ListBox and start App, I see that ListItems that don't have _ in name are displayed.
Why is it?
When I select some items with empty content, it is still same elements with _ symbol.
Try to set the Content property to a TextBlock:
<ListBox HorizontalAlignment="Left" Name="betOdds" VerticalAlignment="Top">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<CheckBox IsChecked="{Binding IsChecked}" HorizontalAlignment="Stretch">
<CheckBox.Content>
<TextBlock Text="{Binding Name}" />
</CheckBox.Content>
</CheckBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
A character preceded by an underscore may be converted to an access key: http://www.c-sharpcorner.com/UploadFile/mahesh/wpf-accesstext/. But you could set the Content to a TextBlock to work around this.
I have my xaml code as :
<ListBox x:Name="SecondListBox" Margin="0,0,-12,0" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<toolkit:ToggleSwitch Name="toggle3" Header="{Binding name}" Height="120" HorizontalAlignment="Left" Margin="35,20,0,0" VerticalAlignment="Center" Width="440" Content="{Binding descrip}" Checked="toggleSwitch1_Checked" Unchecked="toggleSwitch1_Unchecked" Tap="toggleSwitch1_Tap"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PivotItem>
Now i want to retrieve toggleswitch(toggle 3)'s header text in c# code. How can that be done?
When you create a data binding like you did, it should automatically update the variable you bound it to, name
Assuming you have a proper collection to serve as the ItemsSource Binding
the xaml would write as follows:
<ListBox x:Name="SecondListBox" Margin="0,0,-12,0" ItemsSource="{Binding Path=ItemsCollection}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<toolkit:ToggleSwitch Name="toggle3" Header="{Binding Name,Mode=TwoWay}" Height="120" HorizontalAlignment="Left" Margin="35,20,0,0" VerticalAlignment="Center" Width="440" Content="{Binding descrip}" Checked="toggleSwitch1_Checked" Unchecked="toggleSwitch1_Unchecked" Tap="toggleSwitch1_Tap"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You'll have to provide a property named ItemsCollection of type IEnumerable<TModel> in your DataContext
where TModel class shouldcontain Name property
This should put you on the right path to access elements inside of ItemTemplate.Probably that's what your looking for.
http://dotbay.blogspot.in/2009/09/accessing-controls-in-wpf-itemtemplate.html
I have searched extensively on Google and am struggling to find an easy example to follow for an ItemsControl which I think I need to be able to do the following.
I currently have a Grid with a scrollable Listbox containing Checkboxes which works as expected using the following XAML layout
<Grid>
<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Selections}" Margin="12,22,12,94">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}"
Content="{Binding Path=Item.SelectionName}">
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I now want to add a TextBox to the right of each Checkbox so it's aligned horizontally with 1 Checkbox and 1 Textbox per row. I thought I would have to use an ItemsControl to allow two controls but using the ItemsControl as below I lose the ability to scroll
<Grid>
<ItemsControl ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Selections}" Margin="12,22,12,94">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}"
Content="{Binding Path=Item.SelectionName}">
</CheckBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Also, if I try and add a textbox similar to
<DataTemplate>
<CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" />
<TextBox />
</DataTemplate>
I get an error The object 'DataTemplate' already has a child and cannot add 'TextBox'
Essentially, I want it to look like this
Can anyone give me a few pointers on how to structure the controls in XAML to get my desired layout?
Just use a StackPanel in your DataTemplate and set the orientation to horizontal.
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" />
<TextBox />
</StackPanel>
</DataTemplate>