Home > database >  In XAML, how can i set a ENTRY.Text default for a Binding variable?
In XAML, how can i set a ENTRY.Text default for a Binding variable?

Time:06-30

I have a SQLite database with some tables. One of them is a 'Result' table with columns ('unique_number', 'description', 'type', 'units', 'result_entry').

Now in XAML I bind a Results 'ObservableCollection' in a CollectionView:

<CollectionView ItemsSource="{Binding Results}" IsEnabled="{Binding IsBusy}">
  <CollectionView.ItemTemplate>
    <DataTemplate x:DataType="model:Result">
       <Frame>

Created a frame which displays all the info in a grid, with a conditional trigger on < Entry >:

<Label Grid.Column="0" Text="{Binding description}"/>
<Label Grid.Column="0" Text="{Binding type}"/>
<Entry Grid.Column="1" Text="{Binding result_entry}">
  <Entry.Triggers>
    <MultiTrigger TargetType="Entry">
      <MultiTrigger.Conditions>
        <BindingCondition Binding="{Binding type}" Value="time" />
        <BindingCondition Binding="{Binding result_entry}" Value="" />
      </MultiTrigger.Conditions>
      <Setter Property="Text" Value="{Binding Source={x:Static sys:DateTime.Now},StringFormat='{0:HH:mm}'}" />
    </MultiTrigger>
  </Entry.Triggers>
</Entry>

In the case, when a record is shown with 'type' "time" AND no time has been entered. Entry.Text is prefilled with the current time.

Now my problem is, i lose the Binding to the variable 'result_entry'. So when i save the record, the column 'result_entry' is not updated. Is there a way to prefill the Entry WITH the conditions AND maintain the Binding?

Edit: renamed entry to result_entry

CodePudding user response:

You can try to use the DataTrigger in the binding.

Here is the official document:https://docs.microsoft.com/en-us/dotnet/maui/fundamentals/triggers#data-triggers

In addition, you need to change the DataTrigger's setter, such as:

<DataTrigger TargetType="Entry"
                 Binding="{Binding entry}"
                 Value="{x:Null}">
    <Setter Property="Text"
        Value="{Binding Source={x:Static sys:DateTime.Now},StringFormat='{0:HH:mm}'}" />
</DataTrigger>

These lines code means that if the value of the entry is null, set the Entry's text as the current time.

Update

Override the get method, such as:

public string Entry
    {
        
        get
        {
            if(entry != null)
              return entry;
            else
              return DateTime.Now.ToString();
        }
    }

CodePudding user response:

I did manage to solve it, but this solution feels off :|

<Entry Grid.Column="1" Text="{Binding result_entry}" BackgroundColor="LightGrey">
  <Entry.Triggers>
    <DataTrigger TargetType="Entry" Binding="{Binding type}" Value="time">
      <Setter Property="IsVisible" Value="false"/>
       <Setter Property="Text" Value=""/>
    </DataTrigger>
  </Entry.Triggers>
</Entry>
<Entry Grid.Column="1" Text="" BackgroundColor="LightGrey" IsVisible="false">
  <Entry.Triggers>
    <DataTrigger TargetType="Entry" Binding="{Binding type}" Value="time">
      <Setter Property="IsVisible" Value="true"/>
      <Setter Property="Text" Value="{Binding result_entry, Converter={StaticResource NullToTime}}"/>
    </DataTrigger>
  </Entry.Triggers>
</Entry>

Basically I hide and unbind the first Entry when type = 'time' and show and bind the second Entry when type = time, with a 'NullToTime' converter.

  • Related