[英]In XAML, how can i set a ENTRY.Text default for a Binding variable?
我有一個帶有一些表的 SQLite 數據庫。 其中之一是帶有列('unique_number'、'description'、'type'、'units'、'result_entry')的“Result”表。
現在在 XAML 中,我在 CollectionView 中綁定了一個結果“ObservableCollection”:
<CollectionView ItemsSource="{Binding Results}" IsEnabled="{Binding IsBusy}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:Result">
<Frame>
創建了一個在網格中顯示所有信息的框架,在 <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>
在這種情況下,當記錄顯示為“類型”“時間”並且沒有輸入時間時。 Entry.Text 預填充了當前時間。
現在我的問題是,我失去了對變量“result_entry”的綁定。 所以當我保存記錄時,'result_entry' 列沒有更新。 有沒有辦法用條件預先填寫條目並保持綁定?
編輯:將條目重命名為 result_entry
您可以嘗試在綁定中使用DataTrigger
。
這是官方文檔: https ://docs.microsoft.com/en-us/dotnet/maui/fundamentals/triggers#data-triggers
另外,還需要更改DataTrigger的setter,如:
<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>
這些行代碼意味着如果條目的值為空,則將條目的文本設置為當前時間。
更新
覆蓋get方法,如:
public string Entry
{
get
{
if(entry != null)
return entry;
else
return DateTime.Now.ToString();
}
}
我確實設法解決了它,但是這個解決方案感覺不對:|
<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>
基本上,我在 type = 'time' 時隱藏和取消綁定第一個條目,並在 type = time 時顯示並綁定第二個條目,並使用 'NullToTime' 轉換器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.