Solved

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

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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