Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

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

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
ChrisMDrew
Asked:
ChrisMDrew
  • 2
1 Solution
 
MikeTooleCommented:
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
 
ChrisMDrewAuthor Commented:
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
 
MikeTooleCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now