none
Binding questions.

    Question

  • I have created a custom row control with 4 labels within it so I can display data. Each row will require a seperate method to return the data. The problem I am having is in my viewmodel constructor I need to call those 3 methods, but I need to bind each method to a particular row. How would I go about doing that? Is there an easier way?

    Here is some relevant code:

    XAML for creating one row

    <local:TransactionStatusRow x:Name="trans1" Margin="22,95,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="341" />


    XAML for one of the labels (there are 3 more that comprise one row, created with the XAML above)

    <dataInput:Label x:Name="lblTransType" Foreground="White" Margin="4,2,0,3" Content="{Binding TransactionType, Mode=OneWay}" Grid.Column="0" HorizontalContentAlignment="Left" />


     

     Here is the constructor within the viewmodel:

    public TDTransactionStatusViewModel(ITransactionInfoService serviceAgent)
            {
                if (!IsDesignTime)
                {
                    if (serviceAgent != null)
                        ServiceAgent = serviceAgent;
    
                    GetTransInfo();
                    GetBalanceInfo();
                    GetDenialsInfo();
                }
            }


    Here is one of the methods from above:

    public void GetTransInfo()
            {
                ServiceAgent.GetTransInfo(terminals, date, (s, e) => 
                {
                    TransactionType = e.Result.TransactionType;
                    SurchargeFree = e.Result.SurchargeFree.ToString();
                    Surcharge = e.Result.Surcharge.ToString();
                    Total = e.Result.Total.ToString();
                });   
            }


    I guess the question is how do I do this without creating 3 basically duplicate viewmodels for each row I need to create? So I wont have 3 seperate constructors that contain one of the methods above needed to populate that row.

    Monday, April 04, 2011 4:43 PM

Answers

  • Hi,

    Here is the control each row from different source.

    <ItemsControl Grid.Row="1" IsTabStop="False" ItemsSource="{Binding Questions}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Infra:DataTemplateSelector Content="{Binding}" HorizontalContentAlignment="Stretch" IsTabStop="False">
                    <Infra:DataTemplateSelector.Resources>
                        <DataTemplate x:Key="OpenQuestionViewModel">
                            <Views:OpenQuestionView DataContext="{Binding}"/>
                        </DataTemplate>
    
                        <DataTemplate x:Key="MultipleSelectionQuestionViewModel"|
                            <Views:MultipleSelectionView DataContext="{Binding}"/>
                        </DataTemplate>
    
                        <DataTemplate x:Key="NumericQuestionViewModel">
                            <Views:NumericQuestionView DataContext="{Binding}"/>
                        </DataTemplate>
                    </Infra:DataTemplateSelector.Resources>
                </Infra:DataTemplateSelector>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    Thursday, April 07, 2011 7:48 AM

All replies

  • Hi,

    I think that's a design issue for your data entity which bind to your elements.

    We need to implement the INotifyPropertyChanged Interface for your viewmodel class, when property changes, just fire the event to let the system know which property has changed, then bound value will changed automatically. Follow this document please:

    http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(VS.95).aspx

    Tuesday, April 05, 2011 11:13 PM
  • I have a ViewModelBase class that implements INotifyPropertyChanged. I can get data to bind, however, it's not the correct data. Let me see if I can explain this better.

    I created a new "row" control which contains 4 elements to display data. I want multiple rows inside of another user control so it works sort of like a datagrid.

    My first attempt was to try and create one viewmodel which would supply data for each row. The problem I ran into is I couldn't bind specific service methods to an instance of a particular row. So if I had 3 rows I would want 3 seperate service methods to populate each one.

    My second attempt was to create one viewmodel for the base user control that housed the rows. I ran into basically the same problem. I couldn't bind a specific service method to a particular row.

    Now, I'm fairly certain I could make it work if I just created 3 seperate row controls and a view model for each which contained just the service method I needed to populate the row. This just doesn't seem like the right way. There has to be a way to do what I am trying to accomplish right?

     

    Wednesday, April 06, 2011 10:22 AM
  • bind each method to a particular row

    What do you mean?

    Thursday, April 07, 2011 12:47 AM
  • Hi,

    So why don't you use the control like ListBox or DataGrid and design the template of them? I think that's much more easier to approve.

    Something like this:

    http://www.codeproject.com/KB/silverlight/UsingMultipleTemInListBox.aspx

    Thursday, April 07, 2011 4:39 AM
  • Hi,

    Here is the control each row from different source.

    <ItemsControl Grid.Row="1" IsTabStop="False" ItemsSource="{Binding Questions}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Infra:DataTemplateSelector Content="{Binding}" HorizontalContentAlignment="Stretch" IsTabStop="False">
                    <Infra:DataTemplateSelector.Resources>
                        <DataTemplate x:Key="OpenQuestionViewModel">
                            <Views:OpenQuestionView DataContext="{Binding}"/>
                        </DataTemplate>
    
                        <DataTemplate x:Key="MultipleSelectionQuestionViewModel"|
                            <Views:MultipleSelectionView DataContext="{Binding}"/>
                        </DataTemplate>
    
                        <DataTemplate x:Key="NumericQuestionViewModel">
                            <Views:NumericQuestionView DataContext="{Binding}"/>
                        </DataTemplate>
                    </Infra:DataTemplateSelector.Resources>
                </Infra:DataTemplateSelector>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    Thursday, April 07, 2011 7:48 AM
  • Thanks for this. I thought I could make it work a different way and I was wrong. I had someone locally check the code out and he said it wasn't possible to do what I wanted to do the way I was trying to do it. Thanks for the assistance guys. :)

    Thursday, April 07, 2011 11:23 AM
  • Hi,

    You just borrow Prism assembly then you can use the above.

    Friday, April 08, 2011 12:38 AM