UserControl: Use of Custom Control and Events --> XamlParseException (was unhandled)

I have the following UserControl in which I am using some custom controls (UiNumericTextBox - based off of TextBox). When I tried to set the TextChanged property on the UiNumericTextBox, I get the XamlParseException and this Inner Exception:

{"'OnTextChanged' value cannot be assigned to property 'TextChanged' of object 'ACPUi.UiNumericTextBox'. Error binding to target method. Error at object 'ACPUi.UiNumericTextBox'."}

I decided to try using a different custom control (UiButton) and that worked fine. I did not receive any issues. I also tried using the standard controls (TextBox) and that worked fine as well. I'm using the June CTP release.

Here's the XAML for my UserControl and the dummy event methods. Thanks in advance for your assistance.

<UserControlx:Class="ACPUi.UiIPAddress"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:ACPUiNS="clr-namespace:ACPUi">

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinitionWidth="Auto"/>

<ColumnDefinitionWidth="Auto"/>

<ColumnDefinitionWidth="Auto"/>

<ColumnDefinitionWidth="Auto"/>

</Grid.ColumnDefinitions>

<ACPUiNS:UiNumericTextBoxTextChanged="OnTextChanged"x:Name="IPAddressOctet1"Grid.Row="0"Grid.Column="0"Width="32"Height="22">0</ACPUiNS:UiNumericTextBox>

<TextBoxTextChanged="OnTextChanged"Name="IPAddressOctet2"Grid.Row="0"Grid.Column="1"Width="32"Height="22">0</TextBox>

<TextBoxTextChanged="OnTextChanged"Name="IPAddressOctet3"Grid.Row="0"Grid.Column="2"Width="32"Height="22">0</TextBox>

<TextBoxTextChanged="OnTextChanged"Name="IPAddressOctet4"Grid.Row="0"Grid.Column="3"Width="32"Height="22">0</TextBox>

<ACPUiNS:UiButtonClick="OnClick"Grid.Row="0"Grid.Column="0">First</ACPUiNS:UiButton>

</Grid>

</UserControl>

publicvoid OnTextChanged(object sender,RoutedEventArgs e)

{

MessageBox.Show("UiIPAddress:OnTextChanged()");

}

publicvoid OnClick(object sender,RoutedEventArgs e)

{

MessageBox.Show("UiIPAddress:OnClick()");

}

[11721 byte] By [mkfl] at [2007-12-24]
# 1
Hi mkfl, If you change your event-handler code to:

public void OnTextChanged(object sender, TextChangedEventArgs e)

{

MessageBox.Show("UiIPAddress:OnTextChanged()");

}

then your problem will be solved. I think this problem is related to Baml parsing but it's somewhat strange because when you replace your custom control with TextBox it does work properly. I think only MS guys can explain this.

There's still one more way to solve this problem: don't bind OnTextChanged to your custom control's TextChanged property in XAML, but do it in your code:

public UiIPAddress()

{

InitializeComponent();
IPAddressOctet1.TextChanged += new TextChangedEventHandler(OnTextChanged);
...

}

when you do this, you needn't to change your OnTextChanged signature to use TextChangedEventArgs.


BTW, I guess you are in Plantation, right?

TyraelTong at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified