Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WPF ListView ComboBox Bound - Cancel change

Status
Not open for further replies.

Borvik

Programmer
Jan 2, 2002
1,392
US
Ok, I have a ListView in a WPF Window that has a combobox column. The comboboxes have their items source bound to an Enumration, and the value bound to a class property of the item stored in the ListView. This works fine by the way.

I'm doing some data checking in the set method of the property bound and I want to cancel the change. While the class value for this property doesn't actually change - it does appear to have changed in the combobox on the UI.

Here is what I have so far:
Code:
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="AddrPermValues">
    <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="my:CompanyAddressPermissionType" />
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<DataTemplate x:Key="cboAddrPerm">
    <ComboBox ItemsSource="{Binding Source={StaticResource AddrPermValues}}" SelectedValue="{Binding Permission}" Width="120" />
</DataTemplate>
...
<ListView Name="lvCompanyAddresses" Margin="3,3,3,3">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Permission" CellTemplate="{StaticResource cboAddrPerm}" />
            <GridViewColumn Header="Quick Name" DisplayMemberBinding="{Binding QuickName}" Width="Auto" />
            <GridViewColumn Header="Address" DisplayMemberBinding="{Binding FormattedAddress}" Width="Auto" />
            <GridViewColumn Header="Address Type" DisplayMemberBinding="{Binding AddressTypeName}" Width="Auto" />
        </GridView>
    </ListView.View>
</ListView>
Code:
Public Class ClassForItems
    ...
    Private _permission As CompanyAddressPermissionType
    Private _addresstype As AddressType
    ...
    Public Property Permission() As CompanyAddressPermissionType
        Get
            Return _permission
        End Get
        Set(ByVal value As CompanyAddressPermissionType)
            ' add sql for modification
            Dim isBad As Boolean = False
            Select Case value
                Case CompanyAddressPermissionType.Billing
                    isBad = (_addresstype = proj.AddressType.Shipping)
                Case CompanyAddressPermissionType.Shipping
                    isBad = (_addresstype = proj.AddressType.Billing)
                Case CompanyAddressPermissionType.Both
                    isBad = (_addresstype <> proj.AddressType.Both)
            End Select
            If isBad Then
                MsgBox(String.Format("You cannot set permissions to {0} when the address type is {1}.", value.ToString, _addresstype.ToString), MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly, "Invalid Value")
                Exit Property
            End If

            _permission = value
        End Set
    End Property
    ...
End Class

Instead of a message box, I've also tried using throwing an exception (ArguementException). Nothing happened with the exception - so I put in the message box. The message box DOES show, and then exit the property without setting the class variable - but the combobox in the Listview changes regardless.

As far as I can determine because of the way it is setup, I don't have access to the comboboxes Validating event - which is where I might put this for a normal combobox.

Any ideas on how to solve this?

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top