none
请问如何动态的给自定义的style内包含的控件赋值呢 RRS feed

  • 问题

  • 代码如下,我定义了一个grid样式,需要在后台代码中动态的给其中的控件赋值

    应该怎么操作呢,需要动态生成的部分已经标出

    有没有什么好的方法吗?我需要重复的使用这个框架,作为消息格式,如果每次都循环查找节点感觉很麻烦

            <Style x:Key="CustomTable" TargetType="UserControl">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="UserControl">
                            <Grid Name="gFrame">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="30" />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <Image Name="headerImg" Height="30"></Image>//需要赋值的项
                                <Grid Name="gInner">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="10"></RowDefinition>
                                        <RowDefinition></RowDefinition>
                                    </Grid.RowDefinitions>
                                    <TextBlock Name="nickName" ></TextBlock>//需要赋值的项
                                    <Grid Name="gContent">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="10"></ColumnDefinition>
                                            <ColumnDefinition></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <Grid Name="gReal" Grid.Column="1">
                                          //这里需要动态加入一些新的控件  
                                        </Grid>
                                    </Grid>
                                </Grid>
                                
                                
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    2016年4月25日 8:01

答案

  • 您好 wpf萌新,

    同意Bob的观点,我写了一个例子,供你参考。

    1. 为用户控件添加依赖属性用来做绑定。

    public partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            InitializeComponent();
        }
    
        public string HeadImageSource
        {
            get { return (string)GetValue(HeadImageSourceProperty); }
            set { SetValue(HeadImageSourceProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for HeadImageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeadImageSourceProperty =
            DependencyProperty.Register("HeadImageSource", typeof(string), typeof(MyUserControl), new PropertyMetadata(string.Empty));
    
    
    
        public string NickName
        {
            get { return (string)GetValue(NickNameProperty); }
            set { SetValue(NickNameProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for NickName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NickNameProperty =
            DependencyProperty.Register("NickName", typeof(string), typeof(MyUserControl), new PropertyMetadata(string.Empty));
    }

    2. 在模板中绑定依赖属性。

    <Style TargetType="{x:Type local:MyUserControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MyUserControl}">
                    <Grid Name="gFrame">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="30" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Image Name="headerImg" Height="30" Source="{Binding HeadImageSource, RelativeSource={RelativeSource TemplatedParent}}"></Image>
                        <Grid Name="gInner">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="10"></RowDefinition>
                                <RowDefinition></RowDefinition>
                            </Grid.RowDefinitions>
                            <TextBlock Name="nickName"  Text="{Binding NickName, RelativeSource={RelativeSource TemplatedParent}}"></TextBlock>
                            <Grid Name="gContent">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="10"></ColumnDefinition>
                                    <ColumnDefinition></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Grid Name="gReal" Grid.Column="1">
                                    
                                </Grid>
                            </Grid>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年4月26日 3:31

全部回复

  • TemplateBinding UserControl上面自己定义的依赖属性。或者也可以通过附加属性传递值

    Bob Bao

    Do you still use the same Windows 8 LockScreen always? Download Chameleon Win8 App quickly, that changes your LockScreen constantly.
    你是否还在看着一成不变的Windows 8锁屏而烦恼,赶紧下载这个 百变锁屏 应用,让你的锁屏不断地变化起来。

    2016年4月25日 9:59
    版主
  • 有没有例子代码呢,我还没做过这方面的东西
    2016年4月26日 1:23
  • 是需要吧这个customTable里面所有需要赋值的部分都定义成它的依赖属性吗。能不能给点具体提示,那个template binding应该怎么用呢
    2016年4月26日 1:42
  • 您好 wpf萌新,

    同意Bob的观点,我写了一个例子,供你参考。

    1. 为用户控件添加依赖属性用来做绑定。

    public partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            InitializeComponent();
        }
    
        public string HeadImageSource
        {
            get { return (string)GetValue(HeadImageSourceProperty); }
            set { SetValue(HeadImageSourceProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for HeadImageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeadImageSourceProperty =
            DependencyProperty.Register("HeadImageSource", typeof(string), typeof(MyUserControl), new PropertyMetadata(string.Empty));
    
    
    
        public string NickName
        {
            get { return (string)GetValue(NickNameProperty); }
            set { SetValue(NickNameProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for NickName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NickNameProperty =
            DependencyProperty.Register("NickName", typeof(string), typeof(MyUserControl), new PropertyMetadata(string.Empty));
    }

    2. 在模板中绑定依赖属性。

    <Style TargetType="{x:Type local:MyUserControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MyUserControl}">
                    <Grid Name="gFrame">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="30" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Image Name="headerImg" Height="30" Source="{Binding HeadImageSource, RelativeSource={RelativeSource TemplatedParent}}"></Image>
                        <Grid Name="gInner">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="10"></RowDefinition>
                                <RowDefinition></RowDefinition>
                            </Grid.RowDefinitions>
                            <TextBlock Name="nickName"  Text="{Binding NickName, RelativeSource={RelativeSource TemplatedParent}}"></TextBlock>
                            <Grid Name="gContent">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="10"></ColumnDefinition>
                                    <ColumnDefinition></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Grid Name="gReal" Grid.Column="1">
                                    
                                </Grid>
                            </Grid>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年4月26日 3:31
  • 非常感谢!
    2016年4月27日 1:38