Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Binding IsEnabled to count of selected items
Message
De
06/01/2014 15:36:30
 
 
À
06/01/2014 14:43:01
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Versions des environnements
Environment:
C# 4.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01591392
Message ID:
01591417
Vues:
61
>>>Hi,
>>>
>>>I have a delete button on a WPF form which I only want enabled whenever one and only one item is selected in a grid on the same form. I figure I have to do something like this:
>>>
>>>IsEnabled="{Binding ElementName=grdManifest, Path=Items.Selected.Count = 1}"
>>>
>>>but that isn't accepted as valid code.
>>>
>>>Any ideas on how to do this?
>>
>>Implement an IValueConverter (http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter%28v=vs.110%29.aspx) to convert your int value (the count) to a bool. Since its a one way binding, you don't need to implement ConvertBack, just throw a NotImplementedException.
>
>Hi,
>I wondered about using that approach but it's a pretty specific need and I'm fairly sure you'd still need to implement INotifyPropertyChanged so I opted for my suggestion. Can you provide a working example using valueconverter ?
>Best,
>Viv

I forgot that SelectedItems doesn't have a dependency property associated with it, so it can't be bound directly. It can, however, be done using a data trigger:

XAML:
< Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="WpfApplication1.MainWindow"
        xmlns:Converters="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    < Window.Resources>
        < Converters:SelectedEnabledConverter x:Key="SelectedEnabledConverter" />
    < /Window.Resources>
    < Grid>
        < DataGrid x:Name="grdManifest" Margin="0,0,0,43" >
        < /DataGrid>
        < Button Content="Delete" HorizontalAlignment="Left" Margin="119,288,0,0" VerticalAlignment="Top" Width="75" Height="22">
            < Button.Style>
                < Style TargetType="Button">
                    < Style.Triggers>
                        < DataTrigger Binding="{Binding ElementName=grdManifest, Path=SelectedItems.Count, Converter={StaticResource SelectedEnabledConverter}}" Value="false">
                            < Setter Property="IsEnabled" Value="False" />
                        < /DataTrigger>
                    < /Style.Triggers>
                < /Style>
            < /Button.Style>
        < /Button>
    < /Grid>
< /Window>
And converter:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace WpfApplication1
{
    public class SelectedEnabledConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value as int?).GetValueOrDefault() == 1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform