Bonjour,
C'est un problème un peu complexe, je vais essayer d'être plus claire.
Au niveau de l'IHM, il y a un TextBox dans lequel l'utilisateur doit saisir un entier.
Or, pour être valide, cet entier ne doit pas dépasser une certaine limite. Mais cette limite varie, ce n'est pas une constante.
Donc, pour contrôler l'entier saisi par l'utilisateur, je souhaiterais créer une classe qui hériterait de ValidationRule et qui prendrait en paramètre la limite à ne pas dépasser.
J'ai donc cherché une solution sur internet et j'ai trouvé une méthode qui avait l'air pas mal :
http://www.codeproject.com/KB/WPF/At...select=2110158
J'ai créé la classe IntegerContainer, héritant de FrameworkElement :
1 2 3 4 5 6 7 8 9 10 11
| public class IntegerContainer : FrameworkElement
{
public int Value
{
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int), typeof(IntegerContainer), new UIPropertyMetadata(0));
} |
Puis, j'ai créé ma classe de validation :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class IntegerValidationRule : ValidationRule
{
public IntegerContainer Limit { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var intValue = (int)value;
if (intValue > Limit.Value)
{
return new ValidationResult(false, null);
}
return new ValidationResult(true, null);
}
} |
J'ai modifié ma vue :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <UserControl x:Class="WpfApplication1.UserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication1="clr-namespace:WpfApplication1">
<UserControl.Resources>
<FrameworkElement x:Key="DataContextBridge" />
</UserControl.Resources>
<UserControl.DataContext>
<Binding Mode="OneWayToSource" Path="DataContext" Source="{StaticResource DataContextBridge}" />
</UserControl.DataContext>
<Grid>
<ListView ItemsSource="{Binding PolarPoints}" SelectedItem="{Binding SelectedPolarPoint}" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox>
<Binding Path="ValueInKnot">
<Binding.ValidationRules>
<WpfApplication1:IntegerValidationRule>
<WpfApplication1:IntegerValidationRule.Limit>
<WpfApplication1:IntegerContainer DataContext="{Binding Source={StaticResource DataContextBridge}, Path=DataContext}" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.LimitInVM}" />
</WpfApplication1:IntegerValidationRule.Limit>
</WpfApplication1:IntegerValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl> |
Dans mon vue-model, j'ai :
public int LimitInVM { get; set; }
Le souci, c'est dans le code xaml, lorsque je fais
<WpfApplication1:IntegerContainer DataContext="{Binding Source={StaticResource DataContextBridge}, Path=DataContext}" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.LimitInVM}" />
la valeur de LimitInVM est recherchée dans le DataContext spécifié, c'est-à-dire :
1 2 3
| <UserControl.DataContext>
<Binding Mode="OneWayToSource" Path="DataContext" Source="{StaticResource DataContextBridge}" />
</UserControl.DataContext> |
et non dans mon vue-modèle.
J'aimerais donc savoir s'il existe une astuce pour que l'application n'aille pas chercher dans le DataContext défini dans le xaml mais plutôt dans mon vue-modèle.
J'espère que c'est plus clair.
Cécile.
Partager