• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
梦幻仙境的精灵
博客园    首页    新随笔    联系   管理    订阅  订阅

自定义WPF UI控件,并实现binding属性

<UserControl x:Class="CostWPF.UserControls.LabelComboBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d" 
             d:DesignHeight="31.05" d:DesignWidth="360" x:Name="labelComboBox">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="30*"></ColumnDefinition>
            <ColumnDefinition Width="70*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock TextWrapping="Wrap" Text="{Binding Label, ElementName=labelComboBox}" VerticalAlignment="Center"   TextAlignment="Right"></TextBlock>
        <ComboBox Grid.Column="1" Name="CBB_Content" ItemsSource="{Binding Items, ElementName=labelComboBox}" VerticalAlignment="Center" SelectedItem="{Binding Value, ElementName=labelComboBox}"   Margin="10,0,0,0">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Text, ElementName=labelComboBox}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</UserControl>
        public static readonly DependencyProperty TextTProperty = DependencyProperty.Register("Label", typeof(string), typeof(LabelComboBox));
        public string Label
        {
            get { return (string)GetValue(TextTProperty); }
            set
            {
                SetValue(TextTProperty, value);
            }
        }



        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register(
                "Value", typeof(KeyText), typeof(LabelComboBox), new FrameworkPropertyMetadata(null) { BindsTwoWayByDefault = true });
        public KeyText Value
        {
            get { return GetValue(ValueProperty) as KeyText; }
            set
            {
                SetValue(ValueProperty, value);
            }
        }



        public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register(
                "Items", typeof(List<KeyText>), typeof(LabelComboBox), new FrameworkPropertyMetadata(new List<KeyText>()) { BindsTwoWayByDefault = true });
        public List<KeyText> Items
        {
            get { return GetValue(ItemsProperty) as List<KeyText>; }
            set
            {
                SetValue(ItemsProperty, value);
            }
        }

重点:
1、在UserControl上,加一个x:Name, 并在下边的所有绑定里,添加, ElementName=x:Name
2、在后台代码里,需要使用DependencyProperty来定义属性


参考:
https://www.cnblogs.com/mq0036/p/12356454.html

posted @ 2022-07-18 11:03  梦幻仙境的精灵  阅读(345)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3