Solved

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

Posted on 2011-09-30
3
381 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Let me explain this picture a little bit.  First, in case you haven't already guessed, you are looking at my 2 phones, an Android Samsung Galaxy S5 on the left and an iPhone 5 on the right.  They are on their respective cradles on my desk.  But, you…
A short article about problems I had with the new location API and permissions in Marshmallow
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

12 Experts available now in Live!

Get 1:1 Help Now