自定义控件,绑定到背后的代码

马丁·鲍尔

无法绑定到我的代码中用于自定义控件的数据。显然,我必须使用Generic.xaml来设置控件的样式,并且我想绑定到控件中的数据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)));
        }

        private Double _ProfilePhotoSize = 50;
        private Double ProfilePhotoSize
        {
            get { return _ProfilePhotoSize; }
            set
            {
                if (_ProfilePhotoSize != value)
                    _ProfilePhotoSize = value;
            }
        }
    }
}

这是带有绑定的XAML,在不需要时删除了最后一部分:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Controls">

    <Style TargetType="{x:Type local:NumericUpDown}">
        <Style.Resources>
            <SolidColorBrush x:Key="colour1" Color="#434953" />
            <SolidColorBrush x:Key="colour2" Color="#22252b" />
            <SolidColorBrush x:Key="colour3" Color="#606876" />
        </Style.Resources>

        <Setter Property="Width" Value="85" />
        <Setter Property="Margin" Value="5" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:NumericUpDown}">
                    <Border Focusable="{TemplateBinding Focusable}"
                            Width="{TemplateBinding Width}"
                            x:Name="Border">
                        <Grid Focusable="False">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="1*"/>
                                <ColumnDefinition Width="auto"/>
                            </Grid.ColumnDefinitions>

                            <Button x:Name="PART_DecreaseButton" 
                                    Grid.Column="0">
                                <Button.Content>
                                    <Path Data="M0,0 L1,0 0.5,1Z"
                                          Fill="White"
                                          Width="8"
                                          Height="6"
                                          Stretch="Fill"/>
                                </Button.Content>
                                <Button.Template>
                                    <ControlTemplate TargetType="Button">
                                        <Border Name="Border"
                                                Background="{DynamicResource colour1}" 
                                                Width="25" 
                                                Height="25"
                                                CornerRadius="5 0 0 5">
                                            <ContentPresenter />
                                        </Border>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter TargetName="Border" Property="Background" Value="{DynamicResource colour3}" />
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Button.Template>
                            </Button>

                            <TextBox x:Name="PART_TextBox"
                                     Grid.Column="1"
                                     Foreground="White"
                                     Background="{DynamicResource colour2}"
                                     VerticalContentAlignment="Center"
                                     HorizontalContentAlignment="Center"
                                     HorizontalAlignment="Stretch" 
                                     BorderThickness="0"
                                     Focusable="False" Text="0" />

                            <Button x:Name="PART_IncreaseButton" 
                                    Grid.Column="2">
                                <Button.Content>
                                    <Path Data="M0,1 L1,1 0.5,0Z" 
                                          Width="8"
                                          Height="6"
                                          Fill="White" 
                                          Stretch="Fill" />
                                </Button.Content>
                                <Button.Template>
                                    <ControlTemplate TargetType="Button">
                                        <Border Name="Border" 
                                                Background="{DynamicResource colour1}" 
                                                Width="25" 
                                                Height="25"
                                                CornerRadius="0 5 5 0">
                                            <ContentPresenter />
                                        </Border>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter TargetName="Border" Property="Background" Value="{DynamicResource colour3}" />
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Button.Template>
                            </Button>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <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 Path=ProfilePhotoSize, RelativeSource={RelativeSource AncestorType={x:Type local:UserProfile}}}">
...

这是我得到的绑定错误:

BindingExpression路径错误:在“对象”“ UserProfile”(名称=“)”上找不到“ ProfilePhotoSize”属性。BindingExpression:Path = ProfilePhotoSize; DataItem ='UserProfile'(Name =''); 目标元素是“网格”(名称=“ circleGrid”);目标属性为“宽度”(类型为“双精度”)

编辑:刚刚移动了一些代码,现在代码出现问题,我想根据用户制作控件的大小来调整边框大小,所以我制作了一个新的类和属性,将其获取ProfilePhotoSize,然后将其划分一个数字。

背后的代码

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Controls
{
    public class UserProfile : Control
    {
        static UserProfile()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UserProfile),
                new FrameworkPropertyMetadata(typeof(UserProfile)));
        }
    }
    public class UserProfileData : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        private Double _ProfilePhotoSize = 50;
        public Double ProfilePhotoSize
        {
            get { return _ProfilePhotoSize; }
            set
            {
                if (_ProfilePhotoSize != value)
                    _ProfilePhotoSize = value;
                OnPropertyChanged("ProfilePhotoSize");
            }
        }
        private Double _ProfileBorderThickness = 3;
        public Double ProfileBorderThickness
        {
            get { return _ProfileBorderThickness; }
            set
            {
                double x = ProfilePhotoSize / 3;
                if (_ProfileBorderThickness != x)
                    _ProfileBorderThickness = x;
                OnPropertyChanged("ProfileBorderThickness");
            }
        }
    }
}

这是XAML,绑定不再起作用:S

XAML

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Controls">

    <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 ProfilePhotoSize, RelativeSource={RelativeSource AncestorType={x:Type local:UserProfileData}}}">
                        <Grid.RowDefinitions>
pingu2k4

将其更改为公共财产。

    private Double _ProfilePhotoSize = 50;
    public Double ProfilePhotoSize
    {
        get { return _ProfilePhotoSize; }
        set
        {
            if (_ProfilePhotoSize != value)
                _ProfilePhotoSize = value;
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

自定义控件,绑定到背后的代码

来自分类Dev

绑定到子自定义控件的控件模板中父用户控件代码中定义的属性

来自分类Dev

绑定到子自定义控件的控件模板中父用户控件代码中定义的属性

来自分类Dev

WPF自定义控件-绑定到在后台代码中定义的命令

来自分类Dev

将集合绑定到自定义控件属性

来自分类Dev

WPF自定义控件:将CollectionViewSource绑定到DependencyProperty

来自分类Dev

绑定到ItemsControl的DataTemplate内部的自定义控件

来自分类Dev

将集合绑定到自定义控件属性

来自分类Dev

无法将对象的属性绑定到自定义控件

来自分类Dev

将自定义类型属性绑定到自定义控件

来自分类Dev

使用ObservableCollection的自定义控件绑定

来自分类Dev

MVVMCross自定义控件和绑定

来自分类Dev

使用ObservableCollection的自定义控件绑定

来自分类Dev

自定义控件绑定未更新

来自分类Dev

绑定到wpf自定义控件依赖项属性以获取工具提示吗?

来自分类Dev

将ItemsControl中的viewmodel绑定到自定义用户控件

来自分类Dev

动态将自定义控件中的编辑框绑定到托管bean

来自分类Dev

绑定到wpf自定义控件依赖项属性以获取工具提示吗?

来自分类Dev

如何将自定义Dependecy属性绑定到控件的视图模型?

来自分类Dev

无法将ObservableCollection绑定到包含自定义控件的listView

来自分类Dev

将ControlTemplate子项绑定到自定义控件上的DependencyProperty无效

来自分类Dev

将ObservableCollection <int>绑定到自定义控件的IEnumerable <object>

来自分类Dev

将自定义控件类作为ListViewItem绑定到Listview / GridView

来自分类Dev

如何将自定义Dependecy属性绑定到控件的视图模型?

来自分类Dev

将属性绑定到自定义控件的另一个属性

来自分类Dev

将自定义控件中的DependencyProperty绑定到ViewModel属性

来自分类Dev

自定义控件中的属性绑定到依赖项属性不起作用

来自分类Dev

SAPUI5 - 如何获取我的自定义控件绑定到的模型的名称?

来自分类Dev

将通用对象 (List<T>) 绑定到自定义 Xamarin 控件

Related 相关文章

  1. 1

    自定义控件,绑定到背后的代码

  2. 2

    绑定到子自定义控件的控件模板中父用户控件代码中定义的属性

  3. 3

    绑定到子自定义控件的控件模板中父用户控件代码中定义的属性

  4. 4

    WPF自定义控件-绑定到在后台代码中定义的命令

  5. 5

    将集合绑定到自定义控件属性

  6. 6

    WPF自定义控件:将CollectionViewSource绑定到DependencyProperty

  7. 7

    绑定到ItemsControl的DataTemplate内部的自定义控件

  8. 8

    将集合绑定到自定义控件属性

  9. 9

    无法将对象的属性绑定到自定义控件

  10. 10

    将自定义类型属性绑定到自定义控件

  11. 11

    使用ObservableCollection的自定义控件绑定

  12. 12

    MVVMCross自定义控件和绑定

  13. 13

    使用ObservableCollection的自定义控件绑定

  14. 14

    自定义控件绑定未更新

  15. 15

    绑定到wpf自定义控件依赖项属性以获取工具提示吗?

  16. 16

    将ItemsControl中的viewmodel绑定到自定义用户控件

  17. 17

    动态将自定义控件中的编辑框绑定到托管bean

  18. 18

    绑定到wpf自定义控件依赖项属性以获取工具提示吗?

  19. 19

    如何将自定义Dependecy属性绑定到控件的视图模型?

  20. 20

    无法将ObservableCollection绑定到包含自定义控件的listView

  21. 21

    将ControlTemplate子项绑定到自定义控件上的DependencyProperty无效

  22. 22

    将ObservableCollection <int>绑定到自定义控件的IEnumerable <object>

  23. 23

    将自定义控件类作为ListViewItem绑定到Listview / GridView

  24. 24

    如何将自定义Dependecy属性绑定到控件的视图模型?

  25. 25

    将属性绑定到自定义控件的另一个属性

  26. 26

    将自定义控件中的DependencyProperty绑定到ViewModel属性

  27. 27

    自定义控件中的属性绑定到依赖项属性不起作用

  28. 28

    SAPUI5 - 如何获取我的自定义控件绑定到的模型的名称?

  29. 29

    将通用对象 (List<T>) 绑定到自定义 Xamarin 控件

热门标签

归档