mercredi 1 juillet 2015

Expose boolean property in list of of objects to a WPF Checkbox

I have a working a WPF MVVM app. I'm attempting to expose a list of mappable objects called Targets. The Targets are populated after app start. And exposed via a viewModel that is set to a TargetFilter UserControl Data context. In my TargetFilter control I would like a listbox to expose a bool property called ShowOnMap. I was able to expose data via a dictionary, but this is clunky and not a very good way turn targets on an off. Plus it is hacky and breaks all the databinding of MVVM.

public class Target
{
    public int Index { get; set; }
    public int TargetId { get; set; }
    public TargetLocation lastLocation { get; set; }
    //LastLocation contains lat long etc.
    public string Name { get; set}
    public bool ShowOnMap { get; set; }
}

on my user session object I have an ObservableCollection:

public ObservableCollection<Target> TargetCache;

I have view model that exposes this data

public class MapViewModel : ViewModelBase
{
   private ServerSession _serverSession;

    public ObservableCollection<Target> GetTargetCache
    {
        get
        {
            //if (_serverSession.User.TargetCache != null)
            //{
                return _serverSession.User.TargetCache;
            //}
            //return new ObservableCollection<Target>();
        }
        set
        {
            _serverSession.User.TargetCache = value;
        }
    }

    // key value pari of TargetID, TargetName
    public Dictionary<string, string> GetAllTargetDictionary
    {
        get
        {
            return _serverSession.User.AllTargetDictionary;
        }
    }

 // more code

}

This view model it attached to a Settings user Control which has a number of child user controls

    public partial class Settings : UserControl
{
    private MapViewModel _mapViewModel;

    public event EventHandler DismissRequested;

    public Settings()
    {
        InitializeComponent();
    }
    public Settings(MapViewModel mapViewModel)
    {
        InitializeComponent();
        _mapViewModel = mapViewModel;


        TargetFilter targetFilter = new TargetFilter(_mapViewModel.ServerSession);
        targetFilter.DataContext = _mapViewModel;
        targetFilter.DismissRequestedTargetList += new EventHandler(HandleEventHideUserControl);

        TargetFilterTab.Content = targetFilter;

My Xaml is trying to show a combo box and a check box list of Targets.

I have my combo box working and I bind to the dictionary, but I can't bind to the list:

      <StackPanel Grid.Row="0">

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Label Grid.Column="0" Content="Targets" FontWeight="Bold" />
            <Label Grid.Column="1" Name="TargetList" HorizontalContentAlignment="Right" />
        </Grid>
        <TextBlock Grid.Column="0" VerticalAlignment="Center" FontSize="14" Margin="15,0,10,0">Select Case</TextBlock>
        <Separator Margin="10" />
        <ComboBox Grid.Column="1" ItemsSource="{Binding Path=GetCaseDictionary}" 
              DisplayMemberPath="Value" SelectedValuePath="key" 
              SelectedValue="{Binding Path=selectCase}" SelectionChanged="ComboBox_SelectionChanged" Name="CaseDropDown" />


        <TextBlock Grid.Column="0" VerticalAlignment="Center" FontSize="14" Margin="15,0,10,0">Dispaly Targets bind on dictionary</TextBlock>
        <Separator Margin="10" />
        <ListBox ItemsSource="{Binding Path=GetAllTargetDictionary}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <!--<CheckBox IsChecked="{Binding ShowOnMap, Mode = TwoWay}" Click="CheckBox_Click" Name="value" >
                    </CheckBox>-->
                    <CheckBox Content="{Binding Value}"
                 IsChecked="{Binding Path=Key, Mode=OneWay}" Click="CheckBox_Click"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <TextBlock Grid.Column="0" VerticalAlignment="Center" FontSize="14" Margin="15,0,10,0">Display Targets bind on Target Cache</TextBlock>
        <Separator Margin="10" />
        <ListBox ItemsSource="{Binding Path=GetTargetCache}" Name="lbTargets">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <!--<CheckBox IsChecked="{Binding ShowOnMap, Mode = TwoWay}" Click="CheckBox_Click" Name="value" >
                    </CheckBox>-->
                    <CheckBox Content="{Binding Name}"
                 IsChecked="{Binding ShowOnMap}" Click="CheckBox_Click"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>


    </StackPanel>

Here is link to what it looks like with no binding on the final list box tab control image http://ift.tt/1ekExgg

None of the data is present at binding, and is created and accessed later. Why does the binding work with a dictionary but not the list? Any ideas to make my list bind?

Thank you




Aucun commentaire:

Enregistrer un commentaire