locked
userControl RRS feed

  • Question

  • Ok i created a user conrol in expression blend.  I nice see through button.
    I got the click event to work.  then I decided to expose the button text property to the outside world.
    It's been a week and I haven't made any progress and need help desperately.

    On my usercontrol surface I added a button then edit it's ButtonControlTemplate.  Down in the borders and Grids is a ContentPresenter which is where the text of the button is located in the Content property.

    How do i let a wpf application change this property.  I'm using expression blend 2 to create this user control and the only thing I can find on how to do this is through dependencyproperties.  Is this correct.  Cause I have tried this..and get nothing ..no error nudah.

    My problem is that I dont' know how to refence the ContentPresenter object deep inside my control...here is what i got so far

        Public Overloads Property Text() As String
            Get
                Return GetValue(SourceContentProperty).ToString()
            End Get
            Set(ByVal value As String)
                SetValue(SourceContentProperty, value)
            End Set
        End Property
        Public Shared SourceContentProperty As DependencyProperty = DependencyProperty.Register("Content", GetType(String), GetType(GlassButton), New PropertyMetadata("MyDefault", New PropertyChangedCallback(AddressOf ChangeText)))

        Private Shared Sub ChangeText(ByVal source As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
            CType(source, GlassButton).Content = CType(e.NewValue, String)
        End Sub

    The second last line is wrong..have changed GlassButton with ContentPresenter without luck...

    Anyone who can help please jump in

    Erick
    Wednesday, July 22, 2009 7:24 AM

All replies

  • Hi Erick,

    Can you databind the text of the button to a string property ?? or change the the text of the button in the user control where the button lives. I dont think that you need to manipulate the CP directly as this is simplly presenting the content of the text button in this case a text block.

    So you could do something like

    <Button x:Name="MyButton" Content="{Databinding MyButtonText}"/>, where MyButtonText is a public property in the data context attached to your user control.

    or if you want to do it all from code something like

    this.MyButton.Content = "Hello World";


    hope this helps

    <Rich />

    Wednesday, July 22, 2009 10:38 AM
  • Richard thanks for your reply,
               changing the text of the button inside the user control is precisely what I have been trying to do.
    Burried within this usercontrol is a button called ctrButon however it appears that in expression blend you need to use a ContentPresenter to change the Buttons text

    If i access the code Inside my Usercontrol class I am unable to set a reference to underlying button or the Contentpresenter object which are part of the class

    this.ContentPresenter or this.ButtonText  gives me an error of  "not known members"  neither is
    this.ctrButton or this.Button

    If I could reference them than this.ctrButton.Content = "newButtonName" would work.

    Hence my difficulty.  My original code picks up that some one is trying to change the button text.  but I cant' propegate this change down into the button  which is part of my UserControl.  the only expose properties are the UserControl's public properties.  Not those of the of the object which reside on it.  I therefore create a new Property called Text and made it public hoping to use DependencyProperty to propegate the change down into the buttons content property.

    I was under the impression that the  DependencyProperty.Register did just that.  Yet I am stuck on how to set my buttons text when i have no access to the underlying objects even from within the Usercontrol code

    I've been at it for 2 weeks now without a break through.

    Can supply the code if you wish to try.

    Erick
    Thursday, July 23, 2009 12:59 AM
  • Richard thanks for your reply,
               changing the text of the button inside the user control is precisely what I have been trying to do.
    Burried within this usercontrol is a button called ctrButon however it appears that in expression blend you need to use a ContentPresenter to change the Buttons text

    If i access the code Inside my Usercontrol class I am unable to set a reference to underlying button or the Contentpresenter object which are part of the class

    this.ContentPresenter or this.ButtonText  gives me an error of  "not known members"  neither is
    this.ctrButton or this.Button

    If I could reference them than this.ctrButton.Content = "newButtonName" would work.

    Hence my difficulty.  My original code picks up that some one is trying to change the button text.  but I cant' propegate this change down into the button  which is part of my UserControl.  the only expose properties are the UserControl's public properties.  Not those of the of the object which reside on it.  I therefore create a new Property called Text and made it public hoping to use DependencyProperty to propegate the change down into the buttons content property.

    I was under the impression that the  DependencyProperty.Register did just that.  Yet I am stuck on how to set my buttons text when i have no access to the underlying objects even from within the Usercontrol code

    I've been at it for 2 weeks now without a break through.

    Can supply the code if you wish to try.

    Erick
    Can you send over a simple implementation of what you are trying to do.. the other thing is that you are dealing with data in that the text that you are changing is data therefore you can take advantage of data binding.


    But send the code as i dont quite understand the context

    thanks

    <Rich />
    Friday, July 24, 2009 3:10 PM
  • Ok..here is the xaml and the vb code that makes up the glass button control
    you'll need to compile it and created a dll and then create a wpf project and set a reference to button inside the dll

    xaml for control
    <UserControl
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    	mc:Ignorable="d"
    	x:Class="GlassButton"
    	x:Name="UserControl" Width="100" Height="30">
    	<UserControl.Resources>
    		<ControlTemplate x:Key="GlassButton" TargetType="{x:Type Button}">
    			<Grid x:Name="OuterGird">
    				<Rectangle Margin="0,0,0,0" VerticalAlignment="Stretch" StrokeThickness="1" RadiusX="4" RadiusY="4">
    					<Rectangle.Fill>
    						<LinearGradientBrush EndPoint="0.369,0.21" StartPoint="0.675,0.774">
    							<GradientStop Color="#9992C0E8" Offset="0"/>
    							<GradientStop Color="#FF1E86DC" Offset="1"/>
    						</LinearGradientBrush>
    					</Rectangle.Fill>
    					<Rectangle.Stroke>
    						<LinearGradientBrush EndPoint="0.382,0.116" StartPoint="0.618,0.884">
    							<GradientStop Color="#FF2511D8" Offset="0"/>
    							<GradientStop Color="#FFABCCE8" Offset="1"/>
    						</LinearGradientBrush>
    					</Rectangle.Stroke>
    				</Rectangle>
    				<TextBox Margin="0,0,0,0" Text="TextBox" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="0,0,0,0" Foreground="#FFFFFFFF" Background="#00FFFFFF" x:Name="ButtonTextBox"/>
    				<Path Stroke="{x:Null}" Margin="8,3.712,5.029,8.122" Stretch="Fill" StrokeThickness="0" Data="M167.80889,10.198788 C168.06227,8.3587256 167.20925,7.2831486 165.45782,6.9550772 L19.671443,7.637693 C18.742518,7.708315 15.582248,7.6193257 15.429944,7.5239237 15.429944,7.5239237 12.279116,7.979001 12.279116,7.979001 11.119595,8.2070631 10.356582,9.1665193 9.9996476,10.875024 9.9996476,10.875024 9.8747544,13 9.8747544,13 L9.8557733,52.576677 C10.339993,53.918511 11.067782,54.510987 11.915916,55.079524 12.76405,55.648062 14.503288,55.148249 16.548011,54.66758 18.263785,47.574348 165.59877,4.0491713 167.80889,10.198788 z">
    					<Path.Fill>
    						<LinearGradientBrush EndPoint="0.513,1.002" StartPoint="0.517,-0.105">
    							<GradientStop Color="#B2FFFFFF" Offset="0"/>
    							<GradientStop Color="#19FFFFFF" Offset="1"/>
    							<GradientStop Color="#77FFFFFF" Offset="0.382"/>
    							<GradientStop Color="#33FFFFFF" Offset="0.658"/>
    						</LinearGradientBrush>
    					</Path.Fill>
    				</Path>
    			</Grid>
    		</ControlTemplate>
    	</UserControl.Resources>
    
    	<Grid x:Name="LayoutRoot" Height="Auto" Width="Auto">
    		<Button HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" Width="Auto" Height="Auto" Content="Button" Template="{DynamicResource GlassButton}" Background="#FFD4D0C8" x:Name="BaseButton" BorderThickness="0,0,0,0"/>
    	</Grid>
    </UserControl>
    vb code for control

    Imports System
    Imports System.IO
    Imports System.Net
    Imports System.Windows
    Imports System.Windows.Controls
    Imports System.Windows.Data
    Imports System.Windows.Media
    Imports System.Windows.Media.Animation
    Imports System.Windows.Navigation
    
    Partial Public Class GlassButton
    	Public Sub New()
    		MyBase.New()
    
    		Me.InitializeComponent()
            'Me.Content
    		' Insert code required on object creation below this point.
        End Sub
        Public Property ButtonText() As String
            Get
                Return CType(GetValue(ButtonTextProperty), String)
            End Get
            Set(ByVal value As String)
                SetValue(ButtonTextProperty, value)
            End Set
        End Property
    
        Public Shared ReadOnly ButtonTextProperty As DependencyProperty = DependencyProperty.Register("ButtonString", GetType(String), GetType(GlassButton), New FrameworkPropertyMetadata(New PropertyChangedCallback(AddressOf ChangeText)))
    
    
        Private Shared Sub ChangeText(ByVal source As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
          
        End Sub
    
        Private Sub UpdateText(ByVal NewText As String)
    
        End Sub
    
    
    
    End Class


    code to create button in wpf project

    Window x:Class="TestWpf.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cust="clr-namespace:GlassControl;assembly=GlassControl"
            Title="Window1" Height="948" Width="1025" >
        <Canvas>
            <Canvas.Background>
                <ImageBrush ImageSource="Test.jpg"> </ImageBrush>
            </Canvas.Background>
            <Grid ></Grid>
            <cust:GlassButton Height="35" Canvas.Left="550" Canvas.Top="182" Width="121"></cust:GlassButton>
            <Button Canvas.Left="134" Canvas.Top="48" Height="30" Name="button1" Width="100" >Button</Button>
        </Canvas>
    </Window>


    I'm trying to add some properties to the Glass button class The first property I want to expose is the ability to change the text on the button from the wpf project which will be using the GlassButton.  Other properties I would like to expose inlcude one to change the colour of the button and another to ajdust the opacity of the button.

    However, i am unable to even expose the contnet propert (part of the texbox in the control) to the outside. Can you assist

    Regards

    Erick
    Monday, July 27, 2009 12:48 AM