DataTemplate does not show result of bound children - xaml

On my MainWindow.xaml page, I have the following code which works (where MyWord is a string)
<ContentControl Content="{Binding MyWord}" />
I'm playing with DataTemplates and trying understand them. So, I want to reference a DataTemplate from within my ContentControl. The DataTemplate should contain a TextBlock which binds to my string. I updated my code
<ContentControl ContentTemplate="{StaticResource ViewsTemplate}" />
And in my ResourceDictionary I add
<DataTemplate x:Key="ViewsTemplate">
<TextBlock Text="{Binding MyWord}" />
</DataTemplate>
This produces no text on screen at all. I even tried
<ContentControl Content="{Binding MyWord}" ContentTemplate="{StaticResource ViewsTemplate}" />
and still no result on screen.
I can't work out why can any one give some advice please.
Thank you

The ContentControl still needs to have some content bound to it.
<ContentControl ContentTemplate="{StaticResource ViewsTemplate}" Content="{Binding MyWord}" />
Would work, but then you'd need to change your data template because it expects to be able to find MyWord which of course it won't be able to, so you'd want to use just {Binding} instead.
Alternatively, bind the ContentControl's Content to {Binding} - the current DataContext of its parent - and leave the template as it is.

Related

{Binding .} in Xamarin

I saw this piece of code in an example of some Xamarin code:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding .}" TextColor="#66ffff" FontSize="Micro" HorizontalOptions="CenterAndExpand" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
What does Binding . mean? My guess from trying out the code is one character from the ItemsSource like in a regular expression? But I can't find this in the documentation.
The point (.) means that you bind the whole object/ model instead of a single property to the text property of that label. This is sometimes required, for example you need multiple aspects from your model in a converter.
In this code example above, it also could be that the item source for that listview is a list of strings. So there is no property to bind to the text property.

How to change the width of a control dynamically in UWP?

Tabbar is a very common navigation control on iOS and Android. But UWP doesn't seem to have.
I've seen an example XamlPivot(SHORTCUT)①, Use Pivot to make TabBar, The effect is very good, and I tried to modify it, so that the TabBar in the bottom, content in the upper.
My project is MasterDetail, Master part is TabBar(i.e TabsStyle Pivot), Detail part just a blank.
Now I found a big problem, TabBar each item does not automatically divide the width, then I try to use data binding and value converters to dynamically provide the width, the binding source is the ActuallyWidth of the MasterGrid, but the ActuallyWidth does not change with the window size, and when the Window on WideState, the Mater part will become blank.
So, How to change the width of the TabBarItem dynamically?
Various window size effect chart(Remove"()"):
(https:)//i.stack.imgur.com/3GE5t.png
(https:)//i.stack.imgur.com/FyQuX.png
(https:)//i.stack.imgur.com/pChwz.png
(https:)//i.stack.imgur.com/cib1l.png
XAML:
<Pivot x:Name="pivot"
Style="{StaticResource TabsStylePivotStyle}">
<PivotItem>
<PivotItem.Header>
<local:TabHeader Width="{Binding ActualWidth, Converter={StaticResource AutoWidthConverter}, ElementName=pivot, Mode=OneWay}"
Glyph=""
Label="item 1" />
</PivotItem.Header>
<TextBlock Text="Content content content" />
</PivotItem>
<PivotItem>
<PivotItem.Header>
<local:TabHeader Width="{Binding ActualWidth, Converter={StaticResource AutoWidthConverter}, ElementName=pivot, Mode=OneWay}"
Glyph=""
Label="item 2" />
</PivotItem.Header>
<TextBlock Text="Content content content" />
</PivotItem>
<PivotItem>
<PivotItem.Header>
<local:TabHeader Width="{Binding ActualWidth, Converter={StaticResource AutoWidthConverter}, ElementName=pivot, Mode=OneWay}"
Glyph=""
Label="item 3" />
</PivotItem.Header>
<TextBlock Text="Content content content" />
</PivotItem>
</Pivot>
Converter:
public object Convert(object value, Type targetType, object parameter, string language)
{
return (double)value / 3;
}
For the record, ActualWidth is NOT a DependencyProperty in UWP XAML model - so can't participate in bindings properly (it doesn't notify of changes).
So if you wish to do a binding like you're doing, you're going to need to expose ActualWidth in a bindable way. One of the easier ways of doing so is to a create a Behaviour explicitly for that, that attaches to the SizeChange event of the target element (in your case the pivot), and returns it's ActualWidth / ActualHeight / RenderSize as DependencyProperties on the behaviour. Your TabItems would then look to the ActualWidth on the behaviour instead.
(It's not done by default presumably because UWP XAML doesn't have read-only dependency property support, and binding too it can easily lead to circular layout cycles when layout rounding is in play if you're not careful)

Empty collection bound to LongListSelector cause incorrect view of page

I have Windows Phone 8 page which has a number of controls on it, some of them are LongListSelector controls. If all collections have content everything is displayed correctly.
But if any of the collections bound to the lists are empty all controls below them on the page disappear, i.e. the page looks to be truncated as lots of things are missing.
If I add code to make sure to add at least on item to each collection the page display correctly.
The databinding is done using C#, as shown below.
XAML
<phone:LongListSelector Grid.Row="3" x:Name="PicturesGrid">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Source="{Binding Filename}" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
C#
PicturesGrid.ItemsSource = App.ViewModel.SelectedBird.Pictures;
Update: I have noticed that there is a scrollbar present, but however much I scroll I never get down to the bottom.

XAML tostring in ContentControl

I am new to the XAML world and I'm fumbling my way through a lot of tutorials. One thing i'm stuck on is calling the .tostring on an object.
Here is mysetup
I have a listbox that is bound to a list of objects
I have a contentControl bound to the same list that displays the selected item from the listbox.
My ContentControl is as follows:
<ContentControl Grid.Row="1" Margin="0,3,5,204" Name="Detail"
Content="{Binding Source={StaticResource listingDataView}}"
ContentTemplate="{StaticResource myContentTemplate}"
HorizontalAlignment="Right" Width="231"/>
in myContentTemplate I have:
<DataTemplate x:Key="myContentTemplate">
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
<!-- want to call .tostring here-->
</StackPanel>
</DataTemplate>
In the template I'd like to call .tostring on the object that is currently selected but i cant figure out how to do that?
thanks
Steph
well looks like i asked this too soon. I found the answer in another question
How to reference current object in XAML
Quoting the answer from that thread:
According to the Data Binding
Overview, you can use the "/" to
indicate the current item. You can
then navigate up and down the tree as
needs be using the following type
syntaxes:
Button Content="{Binding }" />
Button Content="{Binding Path=/}" />
Button Content="{Binding
Path=/Description}" />
Hope this helps someone else :)

How to sort a Listview in a DataTemplate in XAML only?

I have a window with a TabControl that i've bind to a list of objec, I'll called MyItem :
<TabControl Name="MyTabPNL" Background="Gainsboro"
ItemsSource="{Binding MyItemList, ElementName=WatcherWindow}"
ContentTemplate="{StaticResource tabItemTemplate}">
</TabControl>
This MyItem class has an ObservableCollection, that I want to bind to a Listview, I'm doing this with a DataTemplate.
GOAL: I'd like to sort automatically this ObservableCollection in XAML. Usually i would use a CollectionViewSource, but i can't find a way to this... I've tried stuff like that:
<DataTemplate x:Key="tabItemTemplate">
<DataTemplate.Resources>
<CollectionViewSource x:Key='dayList' Source="{Binding MyDayList}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="MyDate" Direction="Descending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</DataTemplate.Resources>
<Grid >
<ListView ItemsSource="{Binding Source={StaticResource dayList}}" >
<ListView.View>
<GridView x:Name="gridvwDay" >
<GridViewColumn Header="MyDate"
CellTemplate="{StaticResource myCellTemplatePNLDate}"
HeaderContainerStyle="{StaticResource CustomHeaderStyleNeutral}"
Width="70" />
</GridView>
</ListView.View>
</ListView>
</Grid>
But everytime i've got the same error:
System.Windows.Data Error: 2 : Cannot
find governing FrameworkElement or
FrameworkContentElement for target
element.
BindingExpression:Path=MyDayList;
DataItem=null; target element is
'CollectionViewSource'
(HashCode=58368655); target property
is 'Source' (type 'Object')
I can't find a way to make a link between the dayList in the ListView ItemsSource, and the dayList in the CollectionRessource.
Do you guys have an idea?
FYI: pre sorting the ObservableCollection is not doable, because of the nature of the Class i'm using.
Have you tried simply,
<ListView ItemsSource="{StaticResource dayList}">
As per the doc: http://msdn.microsoft.com/en-us/library/ms750950.aspx
You don't need to bind when its just static on the page :)