Asked by:
How to add TEXT property to one Inherit control?

Question
-
Hi fellas,
I am just pursuing add Text method to my UserControl in order to be able to assign values (not only recover them).
Code-behind for User Control:
Public Class CountryDropdown Inherits UserControl Public Shared ReadOnly SelectedCountryProperty As DependencyProperty = DependencyProperty.Register("SelectedCountry", GetType([String]), GetType(CountryDropdown), New UIPropertyMetadata(Nothing)) Public Shared ReadOnly CountriesProperty As DependencyProperty = DependencyProperty.Register("Countries", GetType(DataTable), GetType(CountryDropdown), New UIPropertyMetadata(Nothing)) Public Sub New() InitializeComponent() Countries = GetData() End Sub Public Property Countries() As DataTable Get Return DirectCast(GetValue(CountriesProperty), DataTable) End Get Set(value As DataTable) SetValue(CountriesProperty, value) End Set End Property Public Property SelectedCountry() As [String] Get Return DirectCast(GetValue(SelectedCountryProperty), [String]) End Get Set(value As [String]) SetValue(SelectedCountryProperty, value) End Set End Property Public Function GetData() As DataTable Dim dt As New DataTable() Dim ds As New DataSet() ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "XML\countries.xml") Return ds.Tables(0) End Function End Class
XAML front:
<local:CountryDropdown x:Name="Paisos" Height="36" Grid.Column="1" Margin="0,3,111,37" Grid.Row="8" Grid.ColumnSpan="2"
HorizontalAlignment="Right" Width="288"
Grid.RowSpan="2"/>Code-behind front:
Me.Paisos.Text = "UK"But it does not work at all. So I've put this code unsuccessfully:
Public Property Text() As [String]
Get
Return String.Empty
End Get
Set(value As [String])
SetValue(SelectedCountryProperty, value)
End Set
End PropertyI use this method in order to retrieve by short-code country identificator ("UK", "ES") the row:
Private Function RecuperarNomPais(pais As String) As String
Dim MyDoc As XPathDocument = New XPathDocument(My.Application.Info.DirectoryPath & "\XML\countries.xml")
Dim MyNav As XPathNavigator = MyDoc.CreateNavigator()
Return MyNav.SelectSingleNode("/countries/country[@code='" + pais + "']").ToString
End FunctionThanks in advance for your inputs,
Saturday, October 11, 2014 4:15 PM
All replies
-
First off you are not telling us what the error is (only that it does not work), this does not help us help you so anything we do now is only a guess.
Next follows a sample of a dependency property registration :
Public Shared Function GetGroupControl(ByVal element As DependencyObject) As String If element Is Nothing Then Throw New ArgumentNullException("element") End If Return element.GetValue(GroupControlProperty) End Function Public Shared Sub SetGroupControl(ByVal element As DependencyObject, ByVal value As String) If element Is Nothing Then Throw New ArgumentNullException("element") End If element.SetValue(GroupControlProperty, value) End Sub Public Shared ReadOnly GroupControlProperty As _ DependencyProperty = DependencyProperty.RegisterAttached("GroupControl", GetType(String), GetType(ControlGroupAP), New FrameworkPropertyMetadata(String.Empty, AddressOf GottaGroup))
Note that yours is much different and that is why it does not work.
As for the Text problem you seem to have a half implementation in your code. All dependency registrations should follow what I showed above. If you use the Visual Studio help to insert code it will provide you with the editable template for the code, simply change the name and the type of the dependency property and define what object type (your Usercontrol) can use this.
Hope this helps
LS
Lloyd Sheen
Saturday, October 11, 2014 8:07 PM -
Hi Lloyd, it's always a pleasure read you.
Let me try to explain more cleary my problem.
First of all I retrieve the value on my Sql Table:
Dim pais As String = String.Empty
pais = Me.dgvClientes.SelectedItem("Pais").ToString
And then I assign that value: (remember that "Paisos" is the name of my usercontrol in my XAML page)
Me.Paisos.SelectedCountry = pais
But then as you can see in run-time my UserControl appears empty:
Well, I can choose items without problem. But problem here is everytime I select one row from my left GridView then
this field always appears empty:
Sunday, October 12, 2014 7:09 AM -
There are missing bits that you have to fill up for us to understand the problem.
#1 How do you handle changes to SelectedCountry to reflect on UI?
#2 I can see SelectedCountry property is of string type? Is your item source an IEnumerable<string> as well? (may sound silly, just confirming, 'coz you have more on the screen)
#3 If the answer to question 2 is no, you have to set the selected item as one of the instance bound to the ItemSource collection, (you can't set a reference type outside of item source to be a selected item)
Hope it helps!Regards Vallarasu S. BreakingDotNet.blogspot.com
Sunday, October 12, 2014 8:59 AM -
Hi Vallarasu,
Thanks for your response.
In my Add method I retrieve the info selected this way:
newClient.Pais = RecuperarNomPais(Me.Paisos.SelectedCountry)
newClient.AddNewClient()
My issue is, how can I show the selected country everytime I select one customer in my DataGridView??
This is the function where I retrieve the value:
Private Function RecuperarNomPais(pais As String) As String
Dim MyDoc As XPathDocument = New XPathDocument(My.Application.Info.DirectoryPath & "\XML\countries.xml")
Dim MyNav As XPathNavigator = MyDoc.CreateNavigator()
Return MyNav.SelectSingleNode("/countries/country[@code='" + pais + "']").ToString
End FunctionOn the other hand, tis is the part of of my XML file where .NET takes all the rows:
<?xml version="1.0"?>
-<countries>
<country iso="4" code="AF">Afghanistan</country>
<country iso="8" code="AL">Albania</country>
<country iso="12" code="DZ">Algeria</country>
<country iso="16" code="AS">American Samoa</country>
<country iso="20" code="AD">Andorra</country>
<country iso="24" code="AO">Angola</country>
<country iso="660" code="AI">Anguilla</country>
<country iso="10" code="AQ">Antarctica</country>
<country iso="28" code="AG">Antigua And Barbuda</country>[...]
Sunday, October 12, 2014 10:04 AM