C# 表单编译绑定在DataTemplate上不起作用
我和你有麻烦 我在App.xaml.cs中添加了C# 表单编译绑定在DataTemplate上不起作用,c#,xaml,xamarin,xamarin.forms,data-binding,C#,Xaml,Xamarin,Xamarin.forms,Data Binding,我和你有麻烦 我在App.xaml.cs中添加了XamlComilation 我将我的DataTemplate更改为 <DataTemplate x:Key="RectLayerDataTemplate" x:DataType="{x:Type viewmodels:RectLayerViewModel}"> <forms:RectLayerView forms:ValueX="{Binding ValueX}"
XamlComilation
我将我的DataTemplate
更改为
<DataTemplate x:Key="RectLayerDataTemplate" x:DataType="{x:Type viewmodels:RectLayerViewModel}">
<forms:RectLayerView forms:ValueX="{Binding ValueX}"
forms:ValueY="{Binding ValueY}"
forms:ValueWidth="{Binding ValueWidth}"
forms:ValueHeight="{Binding ValueHeight}"
forms:Color="{Binding Color}" />
</DataTemplate>
您是否使用了
AbsoluteLayout
使布局重叠,因此您认为它不起作用
我用DataTemplate
创建了一个简单的示例,并将其设置为StackLayout
bindablellayout
,效果很好
page.xaml:
<StackLayout x:Name="CanvasLayout" Orientation="Vertical"
BindableLayout.ItemsSource="{Binding LayerViewModels}" />
MyData:
public class MyData
{
public ObservableCollection<RectLayerViewModel> LayerViewModels { set; get; } = new ObservableCollection<RectLayerViewModel>();
public MyData()
{
for (var x = 0; x < 6; x++)
{
LayerViewModels.Add(new RectLayerViewModel { Name = $"Added at {LayerViewModels.Count}", Type = 1 });
}
}
}
数据模板:
public class RectLayerViewModel
{
public string Name { get; set; }
public int Type { get; set; }
}
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local ="clr-namespace:EntryCa"
x:Class="EntryCa.LayerDataTemplate">
<DataTemplate x:Key="RectLayerDataTemplate" x:DataType="local:RectLayerViewModel">
<Label Text="{Binding Name}"
TextColor="Red"
FontAttributes="Bold" />
</DataTemplate>
的示例用法。下面的ItemViewModel
是列表视图中的项目类型。ItemsSource
:
XAML:
你好,@Leo Zhu-MSFT。很抱歉再次打扰你。我已经有了工作示例,它使用了
DataTemplateSelector
。所以我认为绝对布局在这里不是问题。我试图用x:DataType
得到相同的结果,因为我的同事比Xamarin更熟悉WPF。顺便说一下,在你的代码中,我无法得到它s.template
的意思LayerTemplate
是具有多个数据模板的ResourceDictionary。但是,只有一个DataTemplate
应用于所有对象。简言之,有没有一种方法可以通过x:DataType
匹配视图和视图模型,而不使用DataTemplateSelector
?我发现了我需要的相同方法。
public class MyData
{
public ObservableCollection<RectLayerViewModel> LayerViewModels { set; get; } = new ObservableCollection<RectLayerViewModel>();
public MyData()
{
for (var x = 0; x < 6; x++)
{
LayerViewModels.Add(new RectLayerViewModel { Name = $"Added at {LayerViewModels.Count}", Type = 1 });
}
}
}
public class RectLayerViewModel
{
public string Name { get; set; }
public int Type { get; set; }
}
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local ="clr-namespace:EntryCa"
x:Class="EntryCa.LayerDataTemplate">
<DataTemplate x:Key="RectLayerDataTemplate" x:DataType="local:RectLayerViewModel">
<Label Text="{Binding Name}"
TextColor="Red"
FontAttributes="Bold" />
</DataTemplate>
xmlns:vm="clr-namespace:MyViewModels"
...
<DataTemplate x:Key="ListViewItemTemplate" x:DataType="vm:ItemViewModel" >
...
<ListView ... ItemTemplate="{StaticResource ListViewItemTemplate}"/>
namespace MyViewModels
{
public class ItemViewModel : ViewModelPropertyBase
...
public abstract class ViewModelPropertyBase : INotifyPropertyChanged
...