|
Before I start the actual question I do know about these articles:
Fill Combobox With Sorted Enum Without Code[^]
Binding and Using Friendly Enums in WPF[^]
Funky WPF - Enumerations and Combo Boxes[^]
Enum to ComboBox binding[^]
However what I want to do is the folloing. Assume a Class that has an Enum as a Property:
Class Test
Public Enum Something As Integer
<Description("Something happended")> Something
<Description("Nothing happended")> Nothing_
End Enum
Private pEnumer As Something = -1
Public Property Enumer() As Something
Get
Return pEnumer
End Get
Set(ByVal value As Something)
pEnumer = value
End Set
End Property
End Class
And now I want to bind a ComboBox to the Property Enumer, and I thought It would be simple to make an own based on the original ComboBox:
Public Class EnumComboBox
Inherits ComboBox
Private _EnumList As New List(Of EnumerationMember)
Private pEnumItemsSorce As [Enum]
Public Property EnumItemsSorce() As [Enum]
Get
Return pEnumItemsSorce
End Get
Set(ByVal value As [Enum])
pEnumItemsSorce = value
Me.ItemsSource = [Enum].GetValues(value.GetType)
End Set
End Property
Public Function GetEnumItemSource(obj As DependencyObject) As [Enum]
Return EnumItemsSorce
End Function
Public Sub SetEnumItemSource(obj As DependencyObject, value As [Enum])
EnumItemsSorce = value
End Sub
Public Shared ReadOnly EnumItemSourceProperty As DependencyProperty = DependencyProperty.RegisterAttached("EnumItemSource", GetType([Enum]), GetType(EnumComboBox), New PropertyMetadata(Nothing, AddressOf OnEnumItemSourceChanged))
Private Shared Sub OnEnumItemSourceChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim control = TryCast(sender, ItemsControl)
If control IsNot Nothing Then
If e.NewValue IsNot Nothing Then
Dim _enum = [Enum].GetValues(TryCast(e.NewValue, Type))
control.ItemsSource = _enum
End If
End If
End Sub
Protected Overrides Sub OnItemContainerStyleSelectorChanged(oldItemContainerStyleSelector As System.Windows.Controls.StyleSelector, newItemContainerStyleSelector As System.Windows.Controls.StyleSelector)
MyBase.OnItemContainerStyleSelectorChanged(oldItemContainerStyleSelector, newItemContainerStyleSelector)
End Sub
Protected Overrides Sub OnSelectionChanged(e As System.Windows.Controls.SelectionChangedEventArgs)
MyBase.OnSelectionChanged(e)
End Sub
Private _enumType As Type
Private Sub GetValues(ByVal E As [Enum])
Dim result As New List(Of String)
Dim _enumType As Type
_enumType = E.GetType
Dim enumValues = [Enum].GetValues(_enumType)
Dim f = (From enumValue In enumValues Select New EnumerationMember() With { _
.Value = enumValue, _
.Description = enumValue.GetDescription(enumValue) _
}).ToList
_EnumList = f
End Sub
Private Function GetDescription(enumValue As Object) As String
Dim descriptionAttribute = TryCast(_enumType.GetField(enumValue.ToString()).GetCustomAttributes(GetType(ComponentModel.DescriptionAttribute), False).FirstOrDefault(), ComponentModel.DescriptionAttribute)
Return If(descriptionAttribute IsNot Nothing, descriptionAttribute.Description, enumValue.ToString())
End Function
Public Class EnumerationMember
Public Property Description() As String
Get
Return m_Description
End Get
Set(value As String)
m_Description = value
End Set
End Property
Private m_Description As String
Public Property Value() As Object
Get
Return m_Value
End Get
Set(value As Object)
m_Value = value
End Set
End Property
Private m_Value As Object
End Class
End Class
The Idea is to bind the Enum property "Something" dire3ctly to the combobox, mewaning when I change the combobox index the property in the Test class also changes.
Question is: How can I alter the code above to make this happen?
|
|
|
|
|
Kenneth Haugland wrote: mewaning when I change the combobox index the property in the Test class also changes
Ignoring the detail of the rest of your question, I nearly went cross eyed reading VB as it has been so long, this seems to indicate the issue is in your combo binding. Are yo binding your combo to the datacontext in xaml where you should be setting either the SelectedItem or the SelectedValue and the Mode to TwoWay to drive the change in the ViewModel.
Or am I missing something?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Yes I can do that, but that would soon get messy, and I dont really like it (but it works). So I thought I could make a control that could do all of that without any XAML, just by creating a new Control that inherits the ComboBox.
|
|
|
|
|
Kenneth Haugland wrote: without any XAML
Wat, are you nuts, one of the main benefits of SL/WPF is the xaml binding of the data. I've heard of roll your own (I'm a great believer in it) but that is taking it way too far.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Ehhmm... I meant without cluttering up the XAML, not without it
|
|
|
|
|
Ahh good, you were about to be consigned to the nether regions of...
Even so using the SelectedValue/SelectedItem are part of the core binding strategy. Binding to an ENUM on the other hand must have been a challenge and it looks like it "cluttered" up your code somewhat! So is the selection changing your core issue here.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Yes actually, sort of, the values in the combobox stems from the Description of the Enum's and not the enum itself. I actually want to know how do you tell the property in the class, that the selection in hte combobox has change, so you should now update the property.
|
|
|
|
|
I got it working but I had to do thius:
Private Sub cmbEnumComboBox_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles cmbEnumComboBox.SelectionChanged
t.Enumer = cmbEnumComboBox.EnumItemsSorce
End Sub
Now I know that is stupid, how could I do this without this, any ideas?
|
|
|
|
|
I found a solution, so now I can bind an Enum property to a custom combobox without any clutter in Code behind or XAML:
Imports System.ComponentModel
Namespace WPF
Public Class COmboBoxAttached
Inherits ComboBox
Private _EnumList As New List(Of EnumerationMember)
Private DontRebind As Boolean = False
Private pEnumItemsSorce As [Enum]
Public Property EnumItemsSorce() As [Enum]
Get
Return pEnumItemsSorce
End Get
Set(value As [Enum])
If Not DontRebind Then
GetValues(value)
Me.ItemsSource = _EnumList
Me.DisplayMemberPath = "Description"
Me.SelectedValue = "Value"
End If
DontRebind = False
pEnumItemsSorce = value
End Set
End Property
Protected Overrides Sub OnSelectionChanged(e As System.Windows.Controls.SelectionChangedEventArgs)
If Me.SelectedValue IsNot Nothing Then
DontRebind = True
EnumItemsSorce = DirectCast(Me.SelectedValue, EnumerationMember).Value
End If
MyBase.OnSelectionChanged(e)
End Sub
Private _enumType As Type
Private Sub GetValues(ByVal E As [Enum])
Dim result As New List(Of String)
Dim _enumType As Type
_enumType = E.GetType
Dim enumValues = [Enum].GetValues(_enumType)
Dim f = (From enumValue In enumValues Select New EnumerationMember() With { _
.Value = enumValue, _
.Description = DirectCast(enumValue, [Enum]).GetDescription _
}).ToList
_EnumList = f
End Sub
Private Function GetDescription(enumValue As Object) As String
Dim descriptionAttribute = TryCast(_enumType.GetField(enumValue.ToString()).GetCustomAttributes(GetType(ComponentModel.DescriptionAttribute), False).FirstOrDefault(), ComponentModel.DescriptionAttribute)
Return If(descriptionAttribute IsNot Nothing, descriptionAttribute.Description, enumValue.ToString())
End Function
Public Class EnumerationMember
Public Property Description() As String
Get
Return m_Description
End Get
Set(value As String)
m_Description = value
End Set
End Property
Private m_Description As String
Public Property Value() As Object
Get
Return m_Value
End Get
Set(value As Object)
m_Value = value
End Set
End Property
Private m_Value As Object
End Class
End Class
End Namespace
|
|
|
|
|
Hello,
I have a Window and a viewModel class as a data context of the window.
When i try to change some variables in the viewModel class that are binded to Window properties.
I get the next exception:
"Cannot use a DependencyObject that belongs to a different thread than its parent Freezable".
I know that in WPF you dont have access to a UI elements properties from other threads but here i dont directly access the UI element properties i access some variables that are binded to them.
how can i solve this?
|
|
|
|
|
Make sure you are accessing the UI / properties on the right thread.
|
|
|
|
|
Hi Everybody,
Good Day.
Am gonna develop an windows based application in WPF like holding employee profile. It will save the data and also it will have employee image and his/hers parents image. i am using backedn server as SQL and the SQL server is centralized and i will be accessing through internet.
Now i want suggestion that where should i save my images? is it ok to save in image data type of DB server?
or do i need to use any common folder file systems? if as this is an windows application do i need to give any persmissions? so that it will be accessible from windows app?
or is it possible to use google picasa or anything like that? and will it have any licencing issue if i market the application?
please suggest me the best way to save the images from windows application?
thanks in advance.
|
|
|
|
|
If you are using Silverlight, you will not be able to access the file system folder without elevated permissions.
Thus it will be easier for you to store images on the DB server using a web service.
|
|
|
|
|
Dear Abinav,
Thanks for your reply
When you say to store images in database and use web service?
will image will transfer fast in web service? and how to send image via webservice?
or will it work through ADO.NET via internet?
which will be faster?
thanks in advance/
|
|
|
|
|
Store everything in the DB. Do you really want a file share open to the internet?
|
|
|
|
|
Dear Hammer,
THanks for your response. I have some questions on this which i have posted in the above reply.
thanks
|
|
|
|
|
I store the images on a folder on the server and deliver them via the WCF service. There are pros and cons to storing the file in a database, the may be no automatic backup of the file system but you data backup is not bloated by the images.
It would never even occur to be to store images on public server/service like Picassa, too many privacy issues and the images are no longer yours. It also builds in a dependency on yet another 3rd party.
Having a publicly shared folder on the interweb is just crazy!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hello,
I have a window and a user control in side of the window.
I want to set the same ViewModel class as a data context for both,the window and the user control.
how can i do it?
Thanks
|
|
|
|
|
Data contexts are inherited. the data context for a control is automatically passed into all it's children unless it declares a different one for it's own.
So the data context for the window is automatically inherited by your UserControl.
PS: I assume your UserControl is a child of the window
|
|
|
|
|
|
Hi all,
I am newbie to WPF,need your guidance on richtextbox control.My application updating huge texts in richtextbox control,but the text gets overlap
here is my XAML code
<RichTextBox Height="476" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalAlignment="Left" Margin="8,36,0,0" Name="dutRtx" VerticalAlignment="Top" Width="Auto" IsReadOnly="True" FontSize="11" FontFamily="Segoe UI" Cursor="Arrow">
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="4pt"/>
<Setter Property="LineHeight" Value="6.3pt"/>
<Setter Property="FontSize" Value="9pt" />
<Setter Property="LineStackingStrategy" Value="BlockLineHeight" />
</Style>
</RichTextBox.Resources>
<FlowDocument>
<Paragraph FontSize="9pt" LineHeight="4pt" LineStackingStrategy="BlockLineHeight"></Paragraph>
<Paragraph>
</Paragraph>
</FlowDocument>
</RichTextBox>
Increasing the line height the line spacing will be higher.to tackle both line spacing and height...
provide your suggestions....
|
|
|
|
|
So I have spent the last hour or so ratting through Google results to see if there is any news on the future of Silverlight front. This is driven by my decision, 3+ years ago, to move our platform from winforms and click once deployment to Silverlight and a browser based interface.
The newly minted policy that the client could not connect directly to the server dictated WCF. We have been happily grinding out Silverlight apps for the last few years, we are VERY successful as supplying the business solutions.
It is now 2+ years since MS announced the death of SL and I'm pondering our next platform move. I have a wide choice of MS tech, I can go back to winforms, move to WPF and junk the browser altogether, this is sooo tempting.
The thought of going back to html and scripting just curls my toes, JSOPs declaration did not help. To add to the mix we are now getting a small but significant requirement for mobile apps, iOS and Andriod only, windows has no penetration on the mobile platform.
I seriously do not want to take a highly successful dev team down another blind route - again. I am so pissed with MS. But I can't identify an MS unified platform for LOB development and I don't want to take them away from their core skills, what a waste. For the moment we are staying with SL but I need to address mobile development as well.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
SL is dead for browser apps. Never really took off. HTML5 is the future there. WPF is the way to go for desktop apps, but desktop apps are a dying breed except for certain applications. You should have known 3+ yrs ago not to use SL. Writing was on the wall back then... was a proprietary thing from MSFT that only worked in very limited situations at the time.
|
|
|
|
|
You obviously read the wall better than I do, 3 years ago SL was still one of the only viable alternatives to aspx, proprietary or not I was willing to take on an MS platform. I know after 20+ year you would think I would have known better but I waited till SL was well entrenched in the development environment before I changed.
While HTML5 may be cross platform it seems to be an amorphous collection of bits and pieces that includes javascript, which I rate marginally higher than the defunct vb script. I don't want to go to something like Kendo or some other 3rd party framework and have to reskill an entire team on UI development using a minor exposure tool!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
My advice (which is probably worthless, but I'm giving it nonetheless).
JavaScript is a difficult language to learn well and to use well.
It is also a language that anyone can have a play with - and so there is a lot of crap javascript out there.
jQuery helps some - but again there is a big learning curve.
There are also other libraries (KnockoutJS being one I like) which can help.
It sounds like you are being proactive and so have some time before real development takes place.
I would task a dev or two to get into javascript / jQuery / Knockout and produce some exemplar apps - and document them to destruction.
Make sure they have done real-world things with the examples - you have existing code bases on which you can base these, so plop up a web service and re-engineer some of the stuff you have in Js/HTML5
With the luxury of time, you should end up with something you can all comfortably use without going down the 'quick - need to do it NOW even though I don't know it - oh! wait! this works, I'll leave it in the code for now!!' route.
Re-skilling is inevitable, really - so do it over time and do it well.
MVVM # - I did it My Way
___________________________________________
Man, you're a god. - walterhevedeich 26/05/2011
.\\axxx
(That's an 'M')
|
|
|
|