Binding RadioButton.IsChecked with Listbox.SelectedItem
I don't know what i'm doing wrong. I think I might have my ViewModel
wrong, but I'm getting very frustrated because it isn't working. So far
the closest I got is that my View is clickable like a radiobutton with
about 7 other of these radiobuttons. but my listbox is not updating it's
selected property unless I click outside of my radiobutton.
Since i'm new to MVVM as a whole I may be doing this wrong. I found a
contact book example somewhere and am using that as my guide. Basically I
have a AbstractTask as my base. I have 7 child classes. I want to be able
to select one of these AbstractTasks. That's all i'm after. So in my main
window i have this.
<Window x:Class="AdvancedTaskAssigner.View.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="clr-namespace:AdvancedTaskAssigner.View"
Title="MainWindowView" Height="300" Width="300"
SizeToContent="Height">
<DockPanel>
<TextBlock Text="TextBlock" DockPanel.Dock="Top" />
<TextBlock Text="{Binding ElementName=listTasks,
Path=SelectedItem.Name}" DockPanel.Dock="Top" />
<ListBox x:Name="listTasks" ItemsSource="{Binding Tasks}"
HorizontalContentAlignment="Stretch" SelectedItem="{Binding
IsSelected}">
<ListBox.ItemTemplate>
<DataTemplate>
<v:AbstractTaskView />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
</Window>
since this is a library and not a application I had to put this in the
Constructor of MainWindowView
MainWindowView.xaml.cs
public MainWindowView()
{
InitializeComponent();
var atvm = new ViewModel.MainWindowViewModel();
atvm.LoadTasks();
this.DataContext = atvm;
}
MainWindowViewModel.cs
class MainWindowViewModel
{
internal void LoadTasks()
{
var assembly =
Assembly.GetAssembly(typeof(AbstractTask)).GetTypes().Where(t =>
t.IsSubclassOf(typeof(AbstractTask)));
Type[] typelist =
GetTypesInNamespace(Assembly.GetAssembly(typeof(AbstractTask)),
typeof(AbstractTask));
foreach (Type t in typelist)
{
if(!t.IsAbstract && t.BaseType.Equals(typeof(AbstractTask)))
{
tasks.Add(new AbstractTaskViewModel(t));
}
}
}
private Type[] GetTypesInNamespace(Assembly assembly, Type baseClass)
{
return assembly.GetTypes().Where(t =>
t.IsSubclassOf(baseClass)).ToArray();
}
private ObservableCollection<AbstractTaskViewModel> tasks = new
ObservableCollection<AbstractTaskViewModel>();
public ObservableCollection<AbstractTaskViewModel> Tasks
{
get { return tasks; }
}
}
AbstractTaskViewModel.cs
public class AbstractTaskViewModel
{
public AbstractTaskViewModel(Type task)
{
if (!task.IsSubclassOf(typeof(AbstractTask)))
{
throw new NotSupportedException(string.Format("{0} is not a
subclass of AbstractTask", task.Name));
}
Task = task;
}
public string Description
{
get
{
return GetCustomAttribute(0);
}
}
public string Name
{
get
{
return GetCustomAttribute(1);
}
}
public bool IsSelected{get;set;}
private string GetCustomAttribute(int index)
{
var descriptions =
(DescriptionAttribute[])Task.GetCustomAttributes(typeof(DescriptionAttribute),
false);
if (descriptions.Length == 0)
{
return null;
}
return descriptions[index].Description;
}
protected readonly Type Task;
}
AbstractTaskView.xaml
<RadioButton
x:Class="AdvancedTaskAssigner.View.AbstractTaskView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
GroupName="AbstractTasks" Background="Transparent" IsChecked="{Binding
IsSelected, Mode=TwoWay}">
<RadioButton.Template>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Grid>
<Border x:Name="MyHead" BorderBrush="Black"
BorderThickness="2" CornerRadius="5"
Background="LightGray" Margin="20,0,0,0"
HorizontalAlignment="Left" VerticalAlignment="Top"
Panel.ZIndex="2">
<TextBlock Text="{Binding Name}" Margin="5,2"
MinWidth="50" />
</Border>
<Border x:Name="Myoooo" BorderBrush="Black"
BorderThickness="2" CornerRadius="5"
Background="Transparent" Margin="0,10,0,0"
Panel.ZIndex="1">
<TextBlock Text="{Binding Description}"
Margin="5,15,5,2" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="MyHead" Property="Background"
Value="LightGreen" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</RadioButton.Template>
</RadioButton>
this is what i am getting.. and it is frustrating! I want the green border
to be the selected item. not what the Listbox's Selected item is. The
bottom text box is the name of the selected item.
No comments:
Post a Comment