![](/img/trans.png)
[英]How can I bind to dependency properties of a custom control from a resource dictionary defined in Generic.xaml?
[英]Properties of custom control can only be filled by StaticResource but not with values defined in XAML
我已经基于这个https://www.clearpeople.com/insights/blog/2019/February/how-to-create-a-contentview-with-expandable-functionity创建了我自己的ExpandableView
,但作为所有 C# 代码。
我的控件看起来像这样(没有动画部分)
public class ExpandableView : ContentView
{
public static readonly BindableProperty ExpandableContentProperty = BindableProperty.Create(nameof(ExpandableContent), typeof(View), typeof(ExpandableView));
public static readonly BindableProperty TitleTextProperty = BindableProperty.Create(nameof(TitleText), typeof(string), typeof(ExpandableView));
public View ExpandableContent
{
get => this._content;
set
{
if (this._content == value)
{
return;
}
OnPropertyChanging();
if (this._content != null)
{
this._ContentLayout.Children.Remove(this._content);
}
this._content = value;
this._ContentLayout.Children.Add(this._content);
OnPropertyChanged();
}
}
public string TitleText
{
get => this._Title.Text;
set
{
if (this._Title.Text == value)
{
return;
}
OnPropertyChanging();
this._Title.Text = value;
OnPropertyChanged();
}
}
private readonly StackLayout _OuterLayout;
private readonly StackLayout _ContentLayout;
private readonly StackLayout _TitleLayout;
private View _content;
private readonly Label _Title;
public ExpandableView()
{
this._OuterLayout = new StackLayout();
this._ContentLayout = new StackLayout();
this._TitleLayout = new StackLayout
{
Orientation = StackOrientation.Horizontal,
};
this._Title = new Label
{
HorizontalOptions = new LayoutOptions(LayoutAlignment.Start, true),
HorizontalTextAlignment = TextAlignment.Center,
VerticalTextAlignment = TextAlignment.Center,
Text = "Title",
};
this._Title.FontSize = Device.GetNamedSize(NamedSize.Medium, this._Title);
this._TitleLayout.Children.Add(this._Title);
this._OuterLayout.Children.Add(this._TitleLayout);
this._OuterLayout.Children.Add(this._ContentLayout);
Content = this._OuterLayout;
}
}
但是现在,当我像往常一样尝试在 XAML 中使用它时:
<controls:ExpandableView TitleText="Equipment">
<controls:ExpandableView.ExpandableContent>
<StackLayout>
<Label Text="EQ_12345" />
<Button Command="{Binding ShowDatacommand}" />
</StackLayout>
</controls:ExpandableView.ExpandableContent>
</controls:ExpandableView>
将属性设置为某些值会导致标题仍然向我显示“标题”并且没有显示任何内容。 如果我将所有内容都放入StaticResource
一切正常:
<controls:ExpandableView ExpandableContent="{StaticResource ExpendableViewContent}"
TitleText="{StaticResource EquiString}" />
在测试时,我在属性中设置了一些断点,并且仅当我使用{StaticResource}
时才设置属性。 直接在 XAML 中定义的所有值都不会传递给属性。 我在这里做错了什么?
定义您自己的 BindableProperty 属性时,应通过 BindableObject.SetValue/BindableObject.GetValue 访问这些值的最终来源。 Xamarin 运行时可以直接使用它,而不是通过你的 get/set 方法。
以 TitleText 为例,实现应该是这样的:
public string TitleText
{
get => (string)GetValue(TitleTextProperty);
set
{
SetValue(TitleTextProperty, value);
}
}
链接的文章确实做到了这一点。
为了创建属性和显示标题之间的链接,在构造函数中建立数据绑定,将标题标签的Text链接到ExpandableView构造函数中的TitleText属性:
_Title.SetBinding(Label.TextProperty, new Binding(nameof(TitleText)) { Source = this });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.