How to data bind based on emptiness of a collection? - xaml

I want to make a TextBlock visible only when a collection is empty. I have a ListView that is data-bound to the collection already and that's simple. I just want to display something else when the ListView is otherwise empty.
I wrote an IValueConverter that would take the collection, or count, or whatever I need, and return a Visibility appropriately. The XAML looks like this:
<TextBlock Visibility="{Binding Count, ElementName=ContactsList, Converter={StaticResource visibilityWhenEmpty}}"
Text="No contacts yet. Add one using the AppBar below." />
The trouble is that binding just the collection itself only calls my value converter once, when it's empty, and not again when the contents of the collection changes (kinda makes sense). And when, as shown above, I try binding against the collection's Count property, it doesn't call my value converter at all.
Any ideas?

I have faced the same issue. I applied a tricky solution. It might work for you as well. Taks a temporary combo box and bind the collection with that.
<ComboBox x:Name="TempComboBox"
ItemsSource="{Binding DataContext.ContactsList,
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" >
<ComboBox.Template>
<ControlTemplate>
<!--Add file button..-->
<TextBlock Content="Your text..."
>
<TextBlock.Visibility>
<Binding Path="Items.Count"
RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=ComboBox}"
Converter="{StaticResource visibilityWhenEmpty}">
</Binding>
</TextBlock.Visibility>
</TextBlock>
</ControlTemplate>
</ComboBox.Template>
</ComboBox>

You could use notifyPropertyChanged for Count, or implement using BindableBase.

Related

Is it possible to make the background-colour of a Canvas field dependent on its value?

I want one datetime field's background colour to depend on its value. Like if a certain date is passed, the background changes to red.
Is there a way to make this in XAML?
I know there is no possibility of an "if" condition/instruction, but maybe you guys found some way to implement a similar function.
<Canvas Canvas.Left="893" Canvas.Top="208" Height="25" Width="99" Background="red" Panel.ZIndex="-1"/>
<assembly:FieldControl Canvas.Left="890" Canvas.Top="206" FieldControlType="DateControl" FormField="{x:Null}" Height="25" LabelColumnWidth="0" Refnr="123456789" ShowCaption="False" StateImageAlignment="Hidden" Width="106" FontSize="10" Foreground="DimGray"/>
this is my code so far. The Canvas-Part makes the Background go red.
I also tried to put the background property in the "FieldControl" but there it's useless.
EDIT:
After getting the information, that Data Binding could help me with this problem, i tested it like this:
<TextBox Canvas.Left="890" Canvas.Top="226" Name="Date" Width="99" Height="25" VerticalAlignment="Top" Text="{Binding ElementName=Date, Path = SelectedItem.Content, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}" Background="{Binding ElementName=Date, Path=SelectedItem.Content}">
But this is not the direction, i need. Do you have maybe any suggestion, how I can use Data binding to solve my problem?
Yes, it is possible. The concept you need to learn is XAML Data Binding.
You can implement the IValueConverter for this.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters
You can check the value on binding and return the background color.
how to give different text color for each item of listview in xamarin forms

Getting the datarow context or value parent in a xceed grid column group value template?

I am using an xceed DataGrid but I don't believe that matters here. What I need is how to get the databinding correct. I have a DataGrid column as follows:
<xcdg:Column FieldName="TestFieldValue"
Visible="False" Title="TestTitle"
GroupValueTemplate="{StaticResource TestFieldGroupTemplate}"/>
I am using a DataTemplate as follows:
<DataTemplate DataType="{x:Type testNamespace:TestFieldRecord}"
x:Key="TestFieldGroupTemplate">
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type xcdg:DataRow}},
Path=DataContext,
Converter={StaticResource TestFieldValueConverter}}" />
</DataTemplate>
First off, the value the property tied to this column field is just a long. The DataType here on the DataTemplate is actually the parent class of the column field property. An odd thing is if I just bind the text property in the data template like Text={Binding Converter={StaticResource TestFieldValueConverter}}, the converter still gets hit but with the long value from the column.
What I need here is to either bind to the DataRow's context, or to the the parent of the column TestFieldValue, which is the testNamespace:TestFieldRecord. Any find ancestor attempts have led me to no longer hitting my converter.
I think you need to use the Snoop tool to look at the tree and DataContext to see what's happening.
Going up a couple of levels in the tree, the DataContext will be a Xceed Group.
This group has a collection of items.
Your TestFieldGroupTemplate is applying to a Group.
So the main question is where is the group title coming from?
If you just want to change the group text, you can use a DataTemplate targeting Group.
(In the paid version TableflowView this will replace all their standard stuff).
Eg this passes the Group and DataGridControl to an IMultiValueConverter converter:
<DataTemplate DataType="{x:Type xcdg:Group}">
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource AnExampleConverter}">
<Binding Path="DataContext" RelativeSource="{RelativeSource TemplatedParent}"/>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type xcdg:DataGridControl}}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>

UWP - Pivot IsTabStop not working as expected

I have a pivot that I am trying to keep from gaining focus when the user hits the tab key. I immediately tried to set IsTabStop to false. However this wasn't keeping a user from tabbing into the pivot. So I tried testing IsTabStop on two buttons and the behavior was exactly what I expected from the pivot.
Here is my xaml:
<Pivot IsTabStop="False">
<PivotItem Header="Test">
<StackPanel Spacing="10">
<Button Content="Button 1" IsTabStop="True"/>
<Button Content="Button 2" IsTabStop="False"/>
</StackPanel>
</PivotItem>
</Pivot>
Am I missing something here or is there a way around this?
Setting tab index to -1 only makes it first, try something like 1001.. In here they explain the issue you may be having but for RichTextBlock, there are various attributes available for it to set.. https://learn.microsoft.com/en-us/windows/uwp/design/accessibility/keyboard-accessibility
So what i ended up doing is overriding the default style on the pivot. I changed the "HeaderClipper" in the style to have IsTabStop="False", this fixed my problem.
The documentation for the default pivot style can be found at https://msdn.microsoft.com/en-us/library/windows/apps/mt299144.aspx

How to pass an instance of an ancestor control in XAML?

I'm looking to pass a property from the OuterDialog user control, into the InnerDialog as follow:
<uc:OuterDialog x:Name="test" Grid.ColumnSpan="2" Grid.RowSpan="2" Height="768" Width="1366" IsOpen="False">
<uc:StandardDialog Height="768" Width="1366" Grid.ColumnSpan="2">
<uc:InnerDialog AncestorDelegate="AnotherDialog.Close">
</uc:InnerDialog >
</uc:StandardDialog>
</uc:OuterDialog >
Can I do this without resorting to code behind?
How can I get a reference to "closest" ancestor object and pass it on?
The best way to approach it would actually be to have a shared DataContext pointing to a view model that has a property that both controls synchronize with. Other than that you can use {Binding Close, ElementName="AnotherDialog"} to bind to a Close property on an element called AnotherDialog, assuming they are both in the same namescope..

simple data binding to datagrid not happening

i have an datagrid i am trying to bind an value to label which is under datatemplate
but no binding is happening her. ?
<sdk:Label DataContext="{Binding CompanyName}" Width="50" />
i have an column which is above datatemplate there the values are getting binded.
<sdk:DataGridTextColumn Header="CompanyName" Binding="{Binding CompanyName}"/>
but in controls inside data tempalate values are not getting binded.
image field is getting binded with image source what we have specfied.
what is the issue happening here no data binding taking place . help me out
thanks in advance
prince
the issue was the the option what i was using for binding
for label to bind we have to use this property.
Content="{Binding
CompanyName}"