Solved

Windows Mobile 7 - Enabled / Disable a button in a DataBound ListBox / Grid

Posted on 2011-09-30
3
390 Views
Last Modified: 2013-12-27
I have a listbox bound to a data source within which I have a grid which is bound to the items conatined in the data source.

Each item in a grid also has a button which I need to enable/disable based on a field within the Databound object.  I have defined my button as

<Button Content="Log Arrival" Tag="{Binding ID}" IsEnabled="{Binding Arrived}">

Which almost works - really it should be the opposite that is disabled when Arrived is true.  

What is the best way to modify the appearance / state of an item in a databound list?  I have seen mention of using a converter but I am unsure how/if this applies to this issue.
0
Comment
Question by:ChrisMDrew
  • 2
3 Comments
 
LVL 27

Expert Comment

by:MikeToole
ID: 36897898
IsEnabled="{Binding Arrived}" is xaml shorthand for adding a DataTrigger to the control.

The attached code shows how to achieve your aim by using the full syntax to add the DataTrigger.
I have added the style inline to the Button, but it could equally well be defined in a Resources section

As you have read, this could also be achieved by using a Convertor routine, but it requires a little more plumbing.
<Button Content="Log Arrival" Tag="{Binding ID}" >
                <Button.Style>
                    <Style TargetType="Button">
                        <Setter Property="IsEnabled" Value="True"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Arrived}" Value="True">
                                <Setter Property="IsEnabled" Value="False"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>

Open in new window

0
 

Author Comment

by:ChrisMDrew
ID: 36924471
Thanks for this - unfortunately when I try to use it O am tiold that 'triggers' os not found in type 'Style.  I am using Windows Mobile 7
0
 
LVL 27

Accepted Solution

by:
MikeToole earned 500 total points
ID: 36925000
Ah! I answered from a WPF perspective, which doesn't apply to WP7 :(  

From:  http://stackoverflow.com/questions/5647557/style-triggers-in-windows-phone-7
... Silverlight 3 (on which the WP7 framework is based) doesn't support style triggers...
 
Back to Converters :-)
The attached code is a converter class that will negate a boolean.
To use it (in wpf at least)
1. Add a namespace in your xaml to point to the code's namespace, something like
                 xmlns:common="clr-namespace:MyProject.Common"
2. Add a resource entry to create a named instance of the class
               <common:InvertBoolConverter x:Key="InvertBool"/>
3. Use the converter in your binding
                IsEnabled="{Binding Arrived, Converter={StaticResource InvertBool}}"

Hope this helps.



// Converter code
    [ValueConversion(typeof(bool), typeof(bool))]
    public class InvertBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool Value;
            if (!Boolean.TryParse(value.ToString(), out Value))
            {
                return true; // Default to 'true' if the value is invalid
            }
            return !Value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now