[英]In XAML, how can i set a ENTRY.Text default for a Binding variable?
I have a SQLite database with some tables.我有一个带有一些表的 SQLite 数据库。 One of them is a 'Result' table with columns ('unique_number', 'description', 'type', 'units', 'result_entry').
其中之一是带有列('unique_number'、'description'、'type'、'units'、'result_entry')的“Result”表。
Now in XAML I bind a Results 'ObservableCollection' in a CollectionView:现在在 XAML 中,我在 CollectionView 中绑定了一个结果“ObservableCollection”:
<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 >:创建了一个在网格中显示所有信息的框架,在 <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.
Entry.Text 预填充了当前时间。
Now my problem is, i lose the Binding to the variable 'result_entry'.现在我的问题是,我失去了对变量“result_entry”的绑定。 So when i save the record, the column 'result_entry' is not updated.
所以当我保存记录时,'result_entry' 列没有更新。 Is there a way to prefill the Entry WITH the conditions AND maintain the Binding?
有没有办法用条件预先填写条目并保持绑定?
Edit: renamed entry to result_entry编辑:将条目重命名为 result_entry
You can try to use the DataTrigger
in the binding.您可以尝试在绑定中使用
DataTrigger
。
Here is the official document: https://docs.microsoft.com/en-us/dotnet/maui/fundamentals/triggers#data-triggers这是官方文档: 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的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>
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:覆盖get方法,如:
public string Entry
{
get
{
if(entry != null)
return entry;
else
return DateTime.Now.ToString();
}
}
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.基本上,我在 type = 'time' 时隐藏和取消绑定第一个条目,并在 type = time 时显示并绑定第二个条目,并使用 'NullToTime' 转换器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.