Click here to Skip to main content
15,887,175 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: WPF Reporting Question Pin
Bernhard Hiller23-Sep-14 21:56
Bernhard Hiller23-Sep-14 21:56 
GeneralRe: WPF Reporting Question Pin
Kevin Marois24-Sep-14 16:00
professionalKevin Marois24-Sep-14 16:00 
QuestionForm with ComboBoxes bound to the same source. Pin
cjb11016-Sep-14 2:45
cjb11016-Sep-14 2:45 
AnswerRe: Form with ComboBoxes bound to the same source. Pin
Pete O'Hanlon16-Sep-14 3:16
mvePete O'Hanlon16-Sep-14 3:16 
GeneralRe: Form with ComboBoxes bound to the same source. Pin
cjb11016-Sep-14 3:37
cjb11016-Sep-14 3:37 
GeneralRe: Form with ComboBoxes bound to the same source. Pin
Pete O'Hanlon16-Sep-14 6:25
mvePete O'Hanlon16-Sep-14 6:25 
GeneralRe: Form with ComboBoxes bound to the same source. Pin
cjb11017-Sep-14 22:05
cjb11017-Sep-14 22:05 
AnswerRe: Form with ComboBoxes bound to the same source. Pin
cjb11022-Sep-14 4:29
cjb11022-Sep-14 4:29 
Resolved it, adding design time data helped...though weirdly with it working the design time data now looks wrong.

I'm not 100% this is fully correct, or the best or simplest way of doing this...but it does seem to work so far. I've got a few CollectionViewSources, but the first and last are the only two relevant to this.
XML
<Page.Resources>
        <CollectionViewSource x:Key="unitViewSource"
                              Source="{Binding}" />
        <CollectionViewSource x:Key="unitsServicesViewSource"
                              Source="{Binding Path=Services, Source={StaticResource unitViewSource}}" />
        <CollectionViewSource x:Key="unitTypeViewSource"
                              d:DesignSource="{d:DesignData Source=/SampleData/UnitTypeSampleData.xaml}" />
        <CollectionViewSource x:Key="unitNameViewSource" />

I needed the CollectionViewSource unitNameViewSource, so that I could set the ItemsSource on the ComboBox to something. If I didn't set it, or set it to {Binding} I got false values on the first record.

The ComboBoxs are defined as:
XML
<ComboBox Grid.Column="1"
          Grid.Row="1"
          Margin="3"
          Name="businessAreaIdComboBox"
          DisplayMemberPath="Name"
          SelectedValuePath="Id"
          SelectedItem="{Binding Path=BusinessArea}"
          ItemsSource="{Binding Source={StaticResource unitNameViewSource}}">
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
    <ComboBox.InputBindings>
        <KeyBinding Key="Delete"
                    Command="{Binding RelativeSource={RelativeSource AncestorType=Page}, Path=ClearComboBoxCommand}"
                    CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ComboBox}, Path=Name}" />
    </ComboBox.InputBindings>
</ComboBox>

The other thing I think was important was to bind to BusinessArea and not the BusinessAreaId column. Let EF faff with translating that!

My code behind is then:
C#
private void Page_Loaded(object sender, RoutedEventArgs e)
{
    //get the CollectionViewSource object
    viewSource = ((CollectionViewSource) (this.FindResource("unitViewSource")));

    try
    {
        //asynchronous
        BackgroundWorker worker = new BackgroundWorker();
        //the action to run
        worker.DoWork += (s, ev) =>
            {
                this.Dispatcher.Invoke(new Action(() =>
                    {
                        UIHelper.ProgressBarRun(true);
                        this.IsEnabled = false;
                    }));

                context.Units.Load();
                context.UnitTypes.Load();
            };
        //what to run when the above completes
        worker.RunWorkerCompleted += (s, ev) =>
            {
                viewSource.Source = context.Units.Local;
                unitTypeIdComboBox.ItemsSource = context.UnitTypes.Local;

                UpdateComboBoxes();

                this.Dispatcher.Invoke(new Action(() =>
                    {
                        UIHelper.ProgressBarRun(false);
                        navFirstButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                        this.IsEnabled = true;
                    }));
            };
        //start the background work
        worker.RunWorkerAsync();

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void UpdateComboBoxes()
{
    var unitNames = context.Units.Local;
    businessAreaIdComboBox.ItemsSource = unitNames;
    divisionalUnitIdComboBox.ItemsSource = unitNames;
    parentUnitIdComboBox.ItemsSource = unitNames;
}

If I used the unitNameViewSource, and set it (even to the same Units.Local), I got the issue where all three combo boxes displayed the same value. So I need the ViewSource, for the XAML ItemsSource binding, but then overwrite it in code...as I said this probably isn't the right waySmile | :)
The UpdateComboBoxes method is just a hangover from when I thought I would need to refresh them all after adding a new unit.
AnswerSubscribing to the PropertyChangedEventHandler of a class in a List from the class holding the List (solved, sort of...) Pin
GuyThiebaut14-Sep-14 1:25
professionalGuyThiebaut14-Sep-14 1:25 
GeneralRe: Subscribing to the PropertyChangedEventHandler of a class in a List from the class holding the List (solved, sort of...) Pin
SledgeHammer0114-Sep-14 9:41
SledgeHammer0114-Sep-14 9:41 
GeneralRe: Subscribing to the PropertyChangedEventHandler of a class in a List from the class holding the List (solved, sort of...) Pin
GuyThiebaut14-Sep-14 10:06
professionalGuyThiebaut14-Sep-14 10:06 
QuestionWPF Reporting Pin
Kevin Marois9-Sep-14 11:21
professionalKevin Marois9-Sep-14 11:21 
AnswerRe: WPF Reporting Pin
Christian Amado10-Sep-14 5:14
professionalChristian Amado10-Sep-14 5:14 
GeneralRe: WPF Reporting Pin
Kevin Marois15-Oct-14 6:26
professionalKevin Marois15-Oct-14 6:26 
AnswerRe: WPF Reporting Pin
Pete O'Hanlon10-Sep-14 5:57
mvePete O'Hanlon10-Sep-14 5:57 
GeneralRe: WPF Reporting Pin
Kevin Marois21-Oct-14 5:56
professionalKevin Marois21-Oct-14 5:56 
GeneralRe: WPF Reporting Pin
Pete O'Hanlon21-Oct-14 6:05
mvePete O'Hanlon21-Oct-14 6:05 
GeneralRe: WPF Reporting Pin
Kevin Marois21-Oct-14 6:16
professionalKevin Marois21-Oct-14 6:16 
GeneralRe: WPF Reporting Pin
Pete O'Hanlon21-Oct-14 6:18
mvePete O'Hanlon21-Oct-14 6:18 
GeneralRe: WPF Reporting Pin
Kevin Marois21-Oct-14 6:20
professionalKevin Marois21-Oct-14 6:20 
AnswerRe: WPF Reporting Pin
Duncan Edwards Jones21-Oct-14 5:59
professionalDuncan Edwards Jones21-Oct-14 5:59 
AnswerRe: WPF Reporting Pin
Pete O'Hanlon21-Oct-14 7:45
mvePete O'Hanlon21-Oct-14 7:45 
AnswerRe: WPF Reporting Pin
Richard J Foster22-Oct-14 5:55
Richard J Foster22-Oct-14 5:55 
AnswerRe: Textblock with autoscrolling Pin
Richard Deeming5-Sep-14 5:24
mveRichard Deeming5-Sep-14 5:24 
QuestionWPF TreeView ToolTip Problems Pin
Kevin Marois4-Sep-14 7:51
professionalKevin Marois4-Sep-14 7:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.