4 Replies - 5637 Views - Last Post: 03 December 2012 - 08:57 AM Rate Topic: -----

#1 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 267
  • View blog
  • Posts: 547
  • Joined: 07-July 10

User Control that Contains a User Control Binding problems

Posted 03 October 2012 - 12:57 PM

I have a User Control that's just a stylized Combo Box with set items.
TypeSelectControl:
Spoiler


It has a DataContext ViewModel:
Spoiler


I have a different UserControl that has it's own VM class. I'm trying to bind the Person.PersonType to the TypeSelectControl's SelecteType property. The output displays an XML error and the Bind doesn't happen. I know the PersonVM is working since the text boxes are getting bound correctly. Any thoughts on how to fix this?
EditPersonControl:
Spoiler

Error Message:
Spoiler


I'm just frustrated that I don't "get" WPF. WinForms just seams so much more logical and easier to understand. You can also walk through code to find out why it isn't working. WPF doesn't crash, but it does so much behind the curtain you don't know what's going on.

Is This A Good Question/Topic? 0
  • +

Replies To: User Control that Contains a User Control Binding problems

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5438
  • View blog
  • Posts: 11,668
  • Joined: 02-June 10

Re: User Control that Contains a User Control Binding problems

Posted 27 November 2012 - 11:19 AM

You might try DependencyProperty instead of regular property.

I recently came across this myself in a very similar situation.

I have this UserControl
Attached Image


The UserControl has a single Person property.
Each Textbox is bound to a property of that CurrentPerson.

If I use a normal property the GUI doesn't update. If I use a dependency property, they do.

                    <xctk:WatermarkTextBox Name="tbNameFirst"
                                           Margin="0,0,5,0"
                                           DataContext="{Binding Path=CurrentPerson}"
                                           DockPanel.Dock="Left"
                                           TabIndex="0"
                                           Text="{Binding Path=NameFirst,
                                                          Mode=TwoWay}"
                                           Watermark="First name" />



        //private Person _CurrentPerson;
        //public Person CurrentPerson 
        //{
        //    get{ return _CurrentPerson;}
        //    set
        //    {
        //        if (value == _CurrentPerson)
        //            return;
                
        //        _CurrentPerson = value;
        //        RaisePropertyChanged("CurrentPerson");
        //    }
        //}


        public static readonly DependencyProperty CurrentPersonProperty =
            DependencyProperty.Register("CurrentPerson", typeof (Person), typeof (PersonUC), new PropertyMetadata(null));

        public Person CurrentPerson
        {
            get { return (Person)GetValue(CurrentPersonProperty); }
            set
            {
                SetValue(CurrentPersonProperty, value);
                RaisePropertyChanged("CurrentPerson");
            }
        }



There is a shortcut for autofilling a dependacy property: propdp{tab}
Visual Studio will fill in the snippet and you just tab through the primary fields.

Attached Image

Was This Post Helpful? 0
  • +
  • -

#3 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: User Control that Contains a User Control Binding problems

Posted 03 December 2012 - 04:22 AM

tlhIn`toq, maybe you can help me too, as it's a similar issue.

I have a user control which is essentially an image button with 2 states based on the IsEnabled boolean. I'm trying to bind images so I can duplicate the control with multiple buttons, but it doesn't seem to be working. The XAML for the button is:
<Button x:Class="TwoStateButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Click="button_Click" Loaded="Button_Loaded" Height="50" Width="50">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
                <Image Name="NormalImage" Source="{Binding Normal}" Visibility="Visible"/>
                <Image Name="DisabledImage" Source="{Binding Disabled}" Visibility="Hidden"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="NormalImage" Property="Visibility" Value="Hidden"/>
                    <Setter TargetName="DisabledImage" Property="Visibility" Value="Visible"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>


The code for the class:

    public partial class TwoStateButton : Button
    {
        public TwoStateButton()
        {
            InitializeComponent();
        }

        public event RoutedEventHandler Click;

        public ImageSource Normal
        {
            get { return (ImageSource)GetValue(NormalProperty); }
            set { SetValue(NormalProperty, value); }
        }

        public static readonly DependencyProperty NormalProperty =
            DependencyProperty.Register("Normal", typeof(ImageSource), typeof(TwoStateButton), new UIPropertyMetadata(null));

        public ImageSource Disabled
        {
            get { return (ImageSource)GetValue(DisabledProperty); }
            set { SetValue(DisabledProperty, value); }
        }

        public static readonly DependencyProperty DisabledProperty =
            DependencyProperty.Register("Disabled", typeof(ImageSource), typeof(TwoStateButton), new UIPropertyMetadata(null));

        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (Click != null)
                Click(sender, e);
        }

        private void Button_Loaded(object sender, RoutedEventArgs e)
        {

        }

    }


And the XAML to create an object:

<my:TwoStateButton x:Name="buttonFirst" Normal="Images/First.png" Disabled="Images/FirstDisabled.png" Click="buttonFirst_Click" ToolTip="First Page" MinWidth="20"  Visibility="Visible"/>


EDIT: Sorry, I should point out that I've tried a few different ways, but the button's image is not rendering. If I hard code the image's path into the button control it works, but of course I need to change this as I create different button types.

This post has been edited by MrShoes: 03 December 2012 - 04:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: User Control that Contains a User Control Binding problems

Posted 03 December 2012 - 04:36 AM

Ah, I've fixed this. I added the name "twoStateButton" to the control, then changed the image lines like this:
<Image Name="NormalImage" Source="{Binding Path=Normal, ElementName=twoStateButton}" Visibility="Visible"/>

This post has been edited by MrShoes: 03 December 2012 - 05:38 AM

Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5438
  • View blog
  • Posts: 11,668
  • Joined: 02-June 10

Re: User Control that Contains a User Control Binding problems

Posted 03 December 2012 - 08:57 AM

Usually you just use the BoolToVisibilityConverter class against a normal button rather than inheriting a completely new class of button.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1