- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个带有一些自定义属性的自定义控件,但是当尝试绑定(bind)到该属性时它似乎不起作用。
这是我的代码 UserProfile.cs
using System;
using System.Windows;
using System.Windows.Controls;
namespace Controls
{
public class UserProfile : Control
{
static UserProfile()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(UserProfile),
new FrameworkPropertyMetadata(typeof(UserProfile)));
}
#region PhotoSize DP
/// <summary>
/// Gets or sets the Label which is displayed next to the field
/// </summary>
public Double PhotoSize
{
get { return (Double)GetValue(PhotoSizeProperty); }
set { SetValue(PhotoSizeProperty, value); }
}
/// <summary>
/// Identified the Label dependency property
/// </summary>
public static readonly DependencyProperty PhotoSizeProperty =
DependencyProperty.Register("PhotoSize", typeof(Double),
typeof(UserProfile), null);
#endregion
}
}
XAML - Generic.xaml
<Style TargetType="{x:Type local:UserProfile}">
<Setter Property="DataContext" Value="{x:Type local:UserProfile}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontSize" Value="16" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:UserProfile}">
<Grid x:Name="circleGrid" Width="{Binding PhotoSize}">
<Grid.RowDefinitions>
<RowDefinition Height="{Binding Path=ActualWidth, ElementName=circleGrid}" />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Border x:Name="circleBorder"
Grid.Row="0"
CornerRadius="{Binding Path=ActualWidth, ElementName=circleGrid}"
Width="{Binding Path=ActualWidth, ElementName=circleGrid}"
Height="{Binding Path=ActualWidth, ElementName=circleGrid}"
BorderBrush="White"
BorderThickness="3">
<Border.Background>
<ImageBrush ImageSource="D:\Users\Martyn Ball\Pictures\11061728_10153409797331063_2946862347621203654_o.jpg" Stretch="UniformToFill" />
</Border.Background>
</Border>
<WrapPanel Grid.Row="1" HorizontalAlignment="Center">
<TextBlock x:Name="firstName"
FontWeight="Bold"
Foreground="{TemplateBinding Foreground}"
Text="Martyn" />
<TextBlock Text=" "/>
<TextBlock x:Name="lastName"
FontWeight="Normal"
Foreground="{TemplateBinding Foreground}"
Text="Ball" />
</WrapPanel>
<WrapPanel Grid.Row="2">
<WrapPanel Margin="5">
<TextBlock x:Name="imageDifference" Text="" />
<TextBlock Text="56" />
</WrapPanel>
<WrapPanel Margin="5">
<TextBlock x:Name="earningsDifference" Text="£" />
<TextBlock Text="£50.50" />
</WrapPanel>
</WrapPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
错误:
BindingExpression path error: 'PhotoSize' property not found on 'object' ''RuntimeType' (HashCode=19976800)'. BindingExpression:Path=PhotoSize; DataItem='RuntimeType' (HashCode=19976800); target element is 'Grid' (Name=''); target property is 'Width' (type 'Double')
最佳答案
您正在将 DataContext 设置为 Type
(x:Type
正在这样做)。您已设置 TargetType
已经。所以删除行 <Setter Property="DataContext" Value="{x:Type local:UserProfile}" />
并更改 Binding
s 至 TemplateBinding
s,它将起作用。检查this有关 Binding
之间差异的更多信息的问题和 TemplateBinding
关于c# - DependencyProperty 不适用于 CustomControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33829561/
我有一个扩展 Canvas 的类在 WPF 中。 这个类放在一个ScrollViewer . 没有传递对 ScrollViewer 的具体引用进入Canvas , 我想找到 ScrollViewer其
我已经在网上搜索了很多站点,但是没有找到任何解决方案。声明是,UserControl和CustomControl之间没有性能差异。 但是我有以下测试类X,UserControl,CustomContr
如何将 ApplicationCommand.Undo 连接到自定义控件中的处理程序? 一些代码片段将是最受欢迎的...... 最佳答案 this.CommandBindings.Add(new Co
我正在尝试制作一个带有一些自定义属性的自定义控件,但是当尝试绑定(bind)到该属性时它似乎不起作用。 这是我的代码 UserProfile.cs using System; using System
在制作自定义控件时,我试图获取鼠标在 Canvas 上的位置,Canvas 是我的自定义控件内的容器。问题是,当我将我的自定义控件放在测试应用程序中时,只是窗口中的一个网格,我得到的始终是鼠标在窗口本
我正在开发一个自定义控件,它在内部订阅 Touch.FrameReported - 一个静态事件。这有可能导致内存泄漏(在某些情况下确实如此)。 这是我目前的解决方案。在 Loaded/Unloade
我在 WPF 中有一个自定义按钮 ColorPickerButton,并为其应用了样式“ColorPickerButtonStyle”,我必须像这样在 xmal 中应用它;
我正在创建一个具有PropertyChangedCallback依赖项属性的WPF CustomControl。在该Callback方法中,我尝试使用GetTemplateChild()方法从OnAp
我正在尝试将 VideoPreview 自定义控件类中的 IntPtr VidHandle 属性绑定(bind)到其 View 模型 (vm:DebugHiResCameraWindowViewMod
我有多个自定义控件(它是一个下面有标签的按钮)添加到 FLP。我试图通过将一个自定义控件拖到另一个控件上来重新排列添加的自定义控件。在一些帮助下,它可以使用简单的按钮,但不能使用自定义控件。另一个问题
我在自定义控件中设置了自定义属性,如下所示: 然后,我的控件用于我的应用程序 xml 布局,这有效: 但是如果我尝试使用数据绑定(bind)来提供值(value),就像这样: 我
我需要在我的 Angular2+ 组件中集成自定义表单控件: export class HeatMapComponent { ... onMapReady(map: L.Map) { le
我写了一个自定义控件。它是一个带有打开 OpenFileDialog 按钮的文本框。 TextBox 的 Text 属性绑定(bind)到我的依赖属性“FileName”。如果用户通过 OpenFil
这是我提出的 here 的后续问题- 控制类有一个依赖属性—— private static readonly DependencyProperty _Color = DependencyPr
我目前在MVVM中工作,并且为自定义样式的searchTextBox控件创建了一个xaml“TargetType”类。 问题是,我想知道如何将这些文件/在何处放置该文件夹,以便我的项目保持整洁。对于R
我有一个 WPF UserControl - SegmentConrol , 表示一行带有一些文本和一个显示方向 (>) 的数组。 因为我必须自定义此控件样式,所以我决定切换到 CustomContr
我有一个 WPF UserControl - SegmentConrol , 表示一行带有一些文本和一个显示方向 (>) 的数组。 因为我必须自定义此控件样式,所以我决定切换到 CustomContr
我不太清楚何时使用 CustomControl 以及何时使用 UserControl。我基本上知道 CustomControl 允许什么(与模板一起使用时更多自定义)。 我想在 WPF 中制作一个“文
从性能 View 用户控件和自定义控件哪个更好?现在我正在使用用户控件,在特定情况下,我正在创建大约 200 个(大约)此控件的不同实例,但它在加载时有点慢,我需要至少等待 20-30 秒才能完成操作
我快把自己逼疯了...我正在尝试为我的控件提供定义自己的颜色的简单功能。 我的控制: 那是我的 xaml:
我是一名优秀的程序员,十分优秀!