Solved

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

Posted on 2011-09-30
3
406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

752 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