I want to bind the SelectedDate in RadCalendar, to two CurrentDate in different RadScheduleView. How can i do that?
<telerik:RadCalendar Name="radCalendar"
Canvas.Left="80" Canvas.Top="200"
Height="320" Width="400"
SelectedDate="{Binding CurrentDate, ElementName=radScheduleView, Mode=TwoWay}"
SelectionMode="Single" DisplayDate="{Binding DisplayDate, Mode=TwoWay}">
</telerik:RadCalendar>
I want to have two ElementName=radScheduleView and ElementName=radScheduleView1
EDIT
here's my code that need to be bound
<telerik:RadCalendar Name="radCalendar"
Canvas.Left="80" Canvas.Top="200"
Height="320" Width="400"
SelectionMode="Single" DisplayDate="{Binding DisplayDate, Mode=TwoWay}" >
<telerik:RadCalendar.SelectedDate>
<MultiBinding Converter="MultiValueConverter" Mode="TwoWay">
<Binding ElementName="radScheduleView" Path="CurrentDate"/>
<Binding ElementName="radScheduleView1" Path="CurrentDate"/>
</MultiBinding>
</telerik:RadCalendar.SelectedDate>
<telerik:RadScheduleView Name="radScheduleView1"
Canvas.Left="60" Canvas.Top="130"
Height="420" Width="570"
AppointmentsSource="{Binding Appointments}"
SelectedAppointment="{Binding SelectedAppointment, Mode=TwoWay}"
ActiveViewDefinitionIndex="{Binding ActiveViewDefinitionIndex,Mode=TwoWay}"
CurrentDate="{Binding CurrentDate, Mode=TwoWay}">
<telerik:RadScheduleView.ViewDefinitions>
<telerik:DayViewDefinition MinorTickLength="10min" />
</telerik:RadScheduleView.ViewDefinitions>
</telerik:RadScheduleView>
<telerik:RadScheduleView Name="radScheduleView"
Canvas.Left="60" Canvas.Top="130"
Height="420" Width="570"
AppointmentsSource="{Binding Appointments}"
SelectedAppointment="{Binding SelectedAppointment, Mode=TwoWay}"
ActiveViewDefinitionIndex="{Binding ActiveViewDefinitionIndex,Mode=TwoWay}"
CurrentDate="{Binding CurrentDate, Mode=TwoWay}">
<telerik:RadScheduleView.ViewDefinitions>
<telerik:DayViewDefinition MinorTickLength="1h" />
</telerik:RadScheduleView.ViewDefinitions>
You can use MultiBinding instead of simple Binding and use it with the IMultiValueConverter implementation like the following one:
public class MultiValueConverterExtension : MarkupExtension, IMultiValueConverter {
public override object ProvideValue(IServiceProvider serviceProvider) {
return this;
}
object IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return values[1];
}
object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) {
return new object[] { value, value };
}
}
In this case XAML will be like this one:
<telerik:RadCalendar Name="radCalendar"
Canvas.Left="80" Canvas.Top="200"
Height="320" Width="400"
SelectionMode="Single" DisplayDate="{Binding DisplayDate, Mode=TwoWay}">
<telerik:RadCalendar.SelectedDate>
<MultiBinding Converter="{local:MultiValueConverter}" Mode="TwoWay">
<Binding ElementName="radScheduleView" Path="CurrentDate"/>
<Binding ElementName="radScheduleView1" Path="CurrentDate"/>
</MultiBinding>
</telerik:RadCalendar.SelectedDate>
</telerik:RadCalendar>
Related
I want the combo box to be enable when pressing one of the radio buttons.
<RadioButton x:Name="A" GroupName="rButton" Content="A" Grid.Column="4"/>
<RadioButton x:Name="B" GroupName="rButton" Content="B" Grid.Column="4"/>
<RadioButton x:Name="C" GroupName="rButton" Content="C" Grid.Column="4"/>
<RadioButton x:Name="D" GroupName="rButton" Content="D" Grid.Column="4"/>
<ComboBox IsEnabled="{Binding IsChecked,?? }" Grid.Column="5" Width="120" Height="30"/>
If you want to solve this via Bindings (and you should), you need a MultiBindingConverter that returns true as long as one of the values is true (boolean OR):
public class BooleanOrConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
foreach (object value in values)
{
if (value is bool && (bool) value)
return true;
}
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return Enumerable.Repeat(DependencyProperty.UnsetValue, targetTypes.Length).ToArray();
}
}
Definition:
<Window.Resources>
<local:BooleanOrConverter x:Key="OrConverter"/>
</Window.Resources>
Usage:
<RadioButton x:Name="RadioButtonSource" GroupName="rButton" Content="A" Grid.Column="4"/>
<RadioButton x:Name="RadioButtonToken" GroupName="rButton" Content="B" Grid.Column="4"/>
<RadioButton x:Name="RadioButtonII" GroupName="rButton" Content="C" Grid.Column="4"/>
<RadioButton x:Name="RadioButtonUkey" GroupName="rButton" Content="D" Grid.Column="4"/>
<ComboBox Grid.Column="5" Width="120" Height="30">
<ComboBox.IsEnabled>
<MultiBinding Converter="{StaticResource OrConverter}">
<Binding ElementName="RadioButtonSource" Path="IsChecked"/>
<Binding ElementName="RadioButtonToken" Path="IsChecked"/>
<Binding ElementName="RadioButtonII" Path="IsChecked"/>
<Binding ElementName="RadioButtonUkey" Path="IsChecked"/>
</MultiBinding>
</ComboBox.IsEnabled>
</ComboBox>
This way, as soon as any of the RadioButtons's IsChecked properties becomes true, the ComboBox is enabled. If you reset the RadioButtons, it get's disabled again.
I have 2 ListBoxes defined thus:
<ListBox Name="aggregatesListBox" SelectionChanged="aggregatesList_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Amount}"/>
<TextBlock Text="{Binding Path=AccountId}"/>
<TextBlock Text="{Binding Path=Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Name="postingsListBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=PostingId}" />
<TextBlock Text="{Binding Path=Amount}" />
<TextBlock Text="{Binding Path=CreatedDate}" />
<TextBlock Text="{Binding Path=AccountId}" />
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want all items in the postings list to be highlighted (in some way, preferably background colour) if they share the same Account Id as the currently selected aggregated item.
What are my options?
On the advice given I have modified as follows
<ListBox Name="postingsListBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<local:IdToBrushConverter x:Key="IdToBrushConverter" />
</StackPanel.Resources>
<StackPanel.Background>
<MultiBinding Converter="{StaticResource IdToBrushConverter}">
<Binding ElementName="aggregatesListBox" Path="SelectedItem.AccountId"/>
<Binding Path="AccountId"/>
</MultiBinding>
</StackPanel.Background>
<TextBlock Text="{Binding Path=PostingId}" />
<TextBlock Text="{Binding Path=Amount}" />
<TextBlock Text="{Binding Path=CreatedDate}"/>
<TextBlock Text="{Binding Path=AccountId}" />
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and
public class IdToBrushConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
System.Windows.Media.Color colour;
if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue || ((int)values[0] != (int)values[1]))
colour = System.Windows.Media.Colors.White;
else
colour = System.Windows.Media.Colors.CornflowerBlue;
return new SolidColorBrush(colour);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException("");
}
}
An attribute on the Window is required so that the brush converter can be located
xmlns:local="clr-namespace:MyAccountingThing"
I also changed the behind the scenes logic to use a list of Objects as the ItemsSource of each of the 2 Listboxes rather than the DataRowView I had previously.
Sorted - Thanks!
You could use a multibinding with a converter, here's an example.
XAML
<ListBox x:Name="list1"
ItemsSource="{Binding List1}">
</ListBox>
<ListBox x:Name="list2"
ItemsSource="{Binding List2}"
Grid.Column="2">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding .}">
<TextBlock.Background>
<MultiBinding Converter="{StaticResource converter}">
<Binding Path="SelectedItem" ElementName="list1"/>
<Binding Path="."/>
</MultiBinding>
</TextBlock.Background>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
On my silly example, I used the MultiBiding to be able to pass more than one parameter to the Converter, which is the selectedItem on the list1 and the currentItem that ListBox2 is applying the Template, next, I used the converter to compare the received values:
Converter:
public class Converter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var selectedValueList1 = values[0];
var currentItemList2 = values[1];
if(selectedValueList1 == null) // Listbox 1 has no selected Item
return Brushes.Black;
if (selectedValueList1 == currentItemList2)
return Brushes.Red;
return Brushes.Transparent;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
And of course, you have to elaborate better the test on your converter, in my example I just pass two strings to be compared.
And that is it, it works like expected.
I have a kind of annoying scenario here.
In my WPF GUI I declared some RadioButtons, I want the right one to be checked when the GUI loads.
XAML:
<RadioButton Grid.Row="0" Grid.Column="0" Name="RadioButtonShowSettings" GroupName="OnTrayClick" Content="Show settings window" HorizontalAlignment="Left" VerticalAlignment="Center" />
<RadioButton Grid.Row="1" Grid.Column="0" Name="RadioButtonOpenFile" GroupName="OnTrayClick" Content="Open upload dialog" HorizontalAlignment="Left" VerticalAlignment="Center" />
<RadioButton Grid.Row="2" Grid.Column="0" Name="RadioButtonIndexFile" GroupName="OnTrayClick" Content="Open file indexer" HorizontalAlignment="Left" VerticalAlignment="Center" />
<RadioButton Grid.Row="0" Grid.Column="1" Name="RadioButtonImageGallery" GroupName="OnTrayClick" Content="Open image gallery" HorizontalAlignment="Left" VerticalAlignment="Center" />
<RadioButton Grid.Row="1" Grid.Column="1" Name="RadioButtonTakeScreenshot" GroupName="OnTrayClick" Content="Take a screenshot (3 seconds delay)" HorizontalAlignment="Left" VerticalAlignment="Center" />
To keep possible bugs to a minimum I created a Property in my HonkySettings of the type String called TrayIconBehaviour. It contains the ContentProperty of the currently checked RadioButton.
I've been doing the loading programatically with a LoadSettings() function, but I'd like to remove that and do something more appealing with Bindings.
LoadSettings:
private void LoadSettings()
{
List<RadioButton> TrayIconBehaviourRadioButtons = GridTrayIconBehaviour.Children.OfType<RadioButton>().ToList();
foreach (RadioButton rButton in TrayIconBehaviourRadioButtons)
{
if (rButton.Content.Equals(HonkySettings.Default.TrayIconBehaviour))
rButton.IsChecked = true;
}
List<RadioButton> FullscreenCaptureRadioButtons = GridFullscreenCapture.Children.OfType<RadioButton>().ToList();
foreach (RadioButton rButton in FullscreenCaptureRadioButtons)
{
if (rButton.Content.Equals(HonkySettings.Default.FullscreenCapture))
rButton.IsChecked = true;
}
if (RadioButtonQualityPNG.Content.Equals(HonkySettings.Default.ScreenCaptureQuality))
RadioButtonQualityPNG.IsChecked = true;
else RadioButtonQualityJPG.IsChecked = true;
}
I've got HonkyGuiControls, which contains WPF User Controls that I use in my HonkySettingsWindow, if needed, I'd be ready to create a custom RadioButton to bind my settings to them.
I already tried to create a User Control called CustomRadioButton and bind its IsCheckedProperty to something like this:
public Boolean IsChecked
{
get
{
if (CustomRadioButton.Content.Equals(HonkySettings.Default.TrayIconBehaviour)) return true;
else return false;
}
set
{
HonkySettings.Default.TrayIconBehaviour = CustomRadioButton.Content.ToString();
}
}
But the CustomRadioButton IsChecked Property wouldn't bind to it, because I'd need to create a DependencyProperty, and I have no idea how I should do the same thing with a DependencyProperty. Help please.
If you want to this using binding, try this
xaml
<Window x:Class="Stackoverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Stackoverflow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:SettingsConverter x:Key="settingsConverter"/>
</Window.Resources>
<StackPanel>
<RadioButton GroupName="OnTrayClick" Content="Show settings window">
<RadioButton.IsChecked>
<MultiBinding Converter="{StaticResource myNameConverter}">
<Binding Path="HonkySettings.Default.TrayIconBehaviour"/>
<Binding Path="Content" RelativeSource="{RelativeSource Mode=Self}"/>
</MultiBinding>
</RadioButton.IsChecked>
</RadioButton>
<RadioButton GroupName="OnTrayClick" Content="Open upload dialog">
<RadioButton.IsChecked>
<MultiBinding Converter="{StaticResource myNameConverter}">
<Binding Path="HonkySettings.Default.TrayIconBehaviour"/>
<Binding Path="Content" RelativeSource="{RelativeSource Mode=Self}"/>
</MultiBinding>
</RadioButton.IsChecked>
</RadioButton>
<RadioButton GroupName="OnTrayClick" Content="Open file indexer">
<RadioButton.IsChecked>
<MultiBinding Converter="{StaticResource myNameConverter}">
<Binding Path="HonkySettings.Default.TrayIconBehaviour"/>
<Binding Path="Content" RelativeSource="{RelativeSource Mode=Self}"/>
</MultiBinding>
</RadioButton.IsChecked>
</RadioButton>
<RadioButton GroupName="OnTrayClick" Content="Open image gallery">
<RadioButton.IsChecked>
<MultiBinding Converter="{StaticResource myNameConverter}">
<Binding Path="HonkySettings.Default.TrayIconBehaviour"/>
<Binding Path="Content" RelativeSource="{RelativeSource Mode=Self}"/>
</MultiBinding>
</RadioButton.IsChecked>
</RadioButton>
<RadioButton GroupName="OnTrayClick" Content="Take a screenshot (3 seconds delay)">
<RadioButton.IsChecked>
<MultiBinding Converter="{StaticResource myNameConverter}">
<Binding Path="HonkySettings.Default.TrayIconBehaviour"/>
<Binding Path="Content" RelativeSource="{RelativeSource Mode=Self}"/>
</MultiBinding>
</RadioButton.IsChecked>
</RadioButton>
</StackPanel>
Converter
public class SettingsConverter:IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values != null && values.Count() == 2)
return values.First() == values.Last();
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
if you making alot of this kind of binding as i can see there so to reduce xaml code we can do it this way as below
xaml
<Window x:Class="Stackoverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Stackoverflow"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<RadioButton GroupName="OnTrayClick" Content="Show settings window" IsChecked="{local:RadioButtonBinding HonkySettings.Default.TrayIconBehaviour}"/>
<RadioButton GroupName="OnTrayClick" Content="Open upload dialog" IsChecked="{local:RadioButtonBinding HonkySettings.Default.TrayIconBehaviour}"/>
<RadioButton GroupName="OnTrayClick" Content="Open file indexer" IsChecked="{local:RadioButtonBinding HonkySettings.Default.TrayIconBehaviour}"/>
<RadioButton GroupName="OnTrayClick" Content="Open image gallery" IsChecked="{local:RadioButtonBinding HonkySettings.Default.TrayIconBehaviour}"/>
<RadioButton GroupName="OnTrayClick" Content="Take a screenshot (3 seconds delay)" IsChecked="{local:RadioButtonBinding HonkySettings.Default.TrayIconBehaviour}"/>
</StackPanel>
RadioButtonBinding
public class RadioButtonBinding : MultiBinding
{
public RadioButtonBinding(string propName)
{
Bindings.Add(new Binding(propName));
Bindings.Add(new Binding("Content") { RelativeSource = new RelativeSource(RelativeSourceMode.Self) });
Converter = new SettingsConverter();
}
}
converter
public class SettingsConverter:IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values != null && values.Count() == 2)
return values.First() == values.Last();
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
I have a ListBox where I display all order positions. I need to display a price. I created a ValueConverter which takes a OrderPosition object and returns my price as double.
Formula: Amount * Product.Price (Amount and Product are properties in OrderPosition)
My XAML just won't display anything:
<ListBox Grid.Row="1" Grid.Column="0" Margin="3" ItemsSource="{Binding SelectedOrder.OrderPositions}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}x {1}">
<Binding Path="Amount" />
<Binding Path="Product.Label" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Text="{Binding /, Converter={StaticResource PositionPriceConverter}, StringFormat={}{0:c}}" Grid.Column="1"
TextAlignment="Right" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Here is my converter:
public class PositionPriceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var position = (OrderPosition)value;
return position.Amount * position.Product.Price;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
At the moment you set Path=/ which binds it to CollectionView.CurrentItem
When the source is a collection view, the current item can be specified with a slash (/). For example, the clause Path=/ sets the binding to the current item in the view. When the source is a collection, this syntax specifies the current item of the default collection view.
You can achieve what you're after by setting Path=. or not setting Path altogether.
<TextBlock Text="{Binding Path=., Converter=...}
or
<TextBlock Text="{Binding Converter=...}
but be aware that it will not trigger update when either Amount or Product.Price will change so maybe MultiBinding and IMultiValueConverter would be better option.
I Am not sure if that path you provided to the binding is legal ({Binding /, Converter....).
try to change it in:
<TextBlock Text="{Binding Converter={StaticResource PositionPriceConverter}, StringFormat={}{0:c}}" Grid.Column="1" TextAlignment="Right" />
or
<TextBlock Text="{Binding Path=., Converter={StaticResource PositionPriceConverter}, StringFormat={}{0:c}}" Grid.Column="1" TextAlignment="Right" />
<ListBox Grid.Row="1"
Grid.Column="0"
Margin="3"
ItemsSource="{Binding SelectedOrder.OrderPositions}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}x {1}">
<Binding Path="Amount" />
<Binding Path="Product.Label" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Grid.Column="1">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource PositionPriceConverter}" StringFormat="{}{0}x {1}">
<Binding Path="Amount" />
<Binding Path="Product.Label" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Change the converter like this,
public class PositionPriceConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var amt = (double)values[0];
var price = (double) values[1];
return amt * price;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
i have listbox taht fill with list of toggle button in after run the project, if i have observablecollection and i want to compare this ObservableCollection with items in list box where if the item in ObservableCollection exist in listbox i want to make this item (toggle button) checked,
i have tryed to do that but i cant access to toggle button in code behind, becouse the list of toggle buttons show after run the project.
here's my listbox code :
<ListBox x:Name="lbname" ItemsSource="{Binding source}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton x:Name="btnitem" Content="{Binding Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
and my observableCollection :
IQueryable<items> query = _context.items;
ocitems = new ObservableCollection<items>(query);
In short : How can i compare ObservableCollection items with listbox (Buttons) and if item exist in listbox make the Toggle button that represent the item is checked?
hope this clear.
------------------------------------------ More Detail
i have this list box that show choices for selected item, this listBox filled by ObservableCollection "ocSelectedChoice" :
<ListBox x:Name="lbChoices" ItemsSource="{Binding ocSelectedChoice}" DisplayMemberPath="ChoiceName" HorizontalAlignment="Left" Height="165" VerticalAlignment="Top" Width="186" Margin="567,50,0,0" BorderBrush="#FFC1C1C1" Background="#FFE3E3E3" SelectionMode="Extended">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Height" Value="30"/>
<Setter Property="Background" Value="#FFc4d0df"/>
<Setter Property="BorderBrush" Value="#FFC1C1C1"/>
<Setter Property="BorderThickness" Value="0.8"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
when i want to change this item choices, i press the edit button,and will show me windows that has listbox filled by ObservableCollection for all available choices, see the photo, the main problem how to make 'choices 1' lock checked (green one) in choices windows:
<ItemsControl x:Name="icItemGroup" ItemsSource="{Binding PagedSource, ElementName=rdpChoices}" Margin="26,79,0,0" FontWeight="Bold" HorizontalAlignment="Left" Width="506" Height="210" VerticalAlignment="Top" >
<!-- ItemsPanelTemplate -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="4" HorizontalAlignment="left" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!-- ItemTemplate -->
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton x:Name="tbtnChoices" HorizontalAlignment="Left" VerticalAlignment="Top" FontFamily="tahoma" FontSize="12" Height="45" Width="120" FontWeight="Normal" Margin="0,0,0,5" Background="#FFE8E8E8" BorderBrush="#FFC1C1C1" Foreground="#FF6A6A6A"
Content="{Binding ChoiceName}" TabIndex="{Binding ChoicesID}" Click="tbtnChoices_Click">
<ToggleButton.IsChecked>
<MultiBinding Converter="{StaticResource Choices}">
<Binding Path="ocChoice" RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/>
</MultiBinding>
</ToggleButton.IsChecked>
</ToggleButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Converter :
public class ChoicesConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
Choice _choice = values[0] as Choice;
ObservableCollection<Choice> ocChoices = values[1] as ObservableCollection<Choice>;
return ocChoices.Contains(_choice);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture)
{
return null;
}
}
i tried to make this, but sorry still there something unclear for me, please help to solved this issue because it's important for my project.
you should use the multivalue converter to do that i.e bind ToggleButton IsChecked like below:
<ListBox x:Name="lbname" ItemsSource="{Binding source}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton x:Name="btnitem" Content="{Binding Name}">
<ToggleButton.IsChecked>
<MultiBinding Converter="{StaticResource MyConverter}">
<Binding />
<Binding Path="DataContext.ObservableCollectionToCompare" RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/>
</MultiBinding>
</ToggleButton.IsChecked>
</ToggleButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
Here I am assuming that the observalblecollectin to which you want to compare is a property in your view's DataContext.
MyConverter is the multivalue converter that you need to create.
And in the Convert method of your converter you can compare if the item is there in the collection and return true of false accordingly.
public class MyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
Item listItem = values[0] as Item;
ObservableCollection<Item> collection = values[1] as ObservableCollection<Item>;
return collection.Contains(listItem);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture)
{
return null;
}
}
oky, i solved it, here's my code :
This is the converter :
public class IsSelectedChoiceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool _check = false;
if (value == null)
return false;
Item currentItem = (Item)value;
if (currentItem.ChoicesinItem.Count == 0)
_check = false;
foreach (var _choicesinItem in currentItem.ChoicesinItem)
{
if (currentItem.CurrentChoiceId == _choicesinItem.ChoicesId)
_check = true;
}
return _check;
}
and xaml code :
<ToggleButton x:Name="tbtnChoices" HorizontalAlignment="Left" VerticalAlignment="Top" FontFamily="tahoma" FontSize="12" Height="45" Width="120" FontWeight="Normal" Margin="0,0,0,5" Background="#FFE8E8E8" BorderBrush="#FFC1C1C1" Foreground="#FF6A6A6A"
IsChecked="{Binding Path=Item,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IsSelectedChoice}}"
Content="{Binding Item.CurrentChoiceName}" TabIndex="{Binding Item.CurrentChoiceId}" Click="tbtnChoices_Click">
</ToggleButton>
it's work now, Thanks for anyone help me.