Reputation: 1261
Writing a lot of these for a listview. Seems a little bloated to first set the datacontext and the apply binding to the Text
<TextBlock DataContext="{Binding Path=SiteId, Converter={StaticResource siteConverter},ConverterParameter=SiteId}" Text="{Binding Path=SiteName}" ></TextBlock>
Is there to do the binding directly in the text property
EDIT Added full xaml for clarification
<ListView SelectedItem="{Binding SelectedReport}" AlternationCount="2" Grid.Row="2" Name="reportList" ItemsSource="{Binding ReportsView}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch">
<ListView.View>
<GridView>
<GridView.ColumnHeaderContextMenu>
<ContextMenu >
<MenuItem Header="Ascending" />
<MenuItem Header="Descending" />
</ContextMenu>
</GridView.ColumnHeaderContextMenu>
<GridViewColumn Header="Action" CellTemplate="{StaticResource imageCell}" Width="Auto"/>
<GridViewColumn Header="Reportname" Width="Auto" CellTemplate="{StaticResource reportNameCell}"/>
<GridViewColumn Header="Site" Width="Auto" CellTemplate="{StaticResource reportSiteNameCell}"/>
<GridViewColumn Header="Company" Width="Auto" CellTemplate="{StaticResource reportCompanyNameCell}"/>
<GridViewColumn Header="Type" Width="Auto" DisplayMemberBinding="{Binding DocType}"/>
<GridViewColumn Header="Status" Width="Auto" DisplayMemberBinding="{Binding Status}"/>
<GridViewColumn Header="Created by" Width="Auto" DisplayMemberBinding="{Binding CreatedBy}"/>
<GridViewColumn Header="Language" Width="Auto" DisplayMemberBinding="{Binding CreatedBy}"/>
<GridViewColumn Header="Updated" Width="Auto" DisplayMemberBinding="{Binding Updated}"/>
</GridView>
</ListView.View>
</ListView>
EDIT THis is how i load my viewmodel
ReportList = reportListService.Open(filePath);
this.reportsView = CollectionViewSource.GetDefaultView(this.ReportList.Reports);
this.reportsView.SortDescriptions.Add(
new SortDescription("ReportName", ListSortDirection.Ascending));
The ReportList only contains siteid
Upvotes: 0
Views: 213
Reputation: 32505
Ok, well... a couple of things first:
1) Why are you passing in the SiteId
as the ConverterParameter as well as it being the object of the binding. It's redundent... you're passing the same parameter in as the value and parameter of the Convert method?
2) If you are using a ListView
, bind the ItemsSource
to a collection (preferably observable), then use a DataTemplate
as the ListView.ItemTemplate
. If the DataTemplate is reusable across multiple ListView
s, then create it as a resource and set the ItemTemplate
to bind to the resource.
Or... the better solution: Expose things from a view-model that should be. It doesn't sound like "SiteName" is view-related as much as it business-related. So, have your converter logic on your view-model layer instead and expose that additional property.
Upvotes: 2
Reputation: 3909
Yeah you set the datacontext of the View itself to a ViewModel class. Then all you need to do for the controls is:
Text="{Binding StringPropertyToBindTo}"
Upvotes: 0
Reputation: 3972
The easiest way would be implementing a Site
property in your data context which contains the functionality the converter implements currently. Then you can easily bind to Site.SiteName
. Just make sure the data context fires it's PropertyChanged
event for Site
when SiteId
is set to avoid nasty surprises.
However, above approach is impractical if the converter is used all over the place with the same parameter for different data contexts without a common interface or with different base classes.
Upvotes: 0