Hallo zusammen,
kann mir vielleicht jemand erklären wie das mit dem ValidationRule mit Parameterübergabe funktioniert.
Ich habe ein DataGrid mit einer Textbox und einer Combobox, die Textbox soll validiert werden das klappt auch nur schaffe ich es nicht das ich den Parameter aus einer Property übergabe also dynamisch statisch funktionierts was mache bloss falsch?
Hier ein Codeausschnitt
<DataGrid DockPanel.Dock="Top" x:Name="dataGridNewJobs" Margin="4" AutoGenerateColumns="False"
ItemsSource="{Binding NewJobsNewAfo}" CanUserAddRows="False" SelectedItem="{Binding SelectedNewJobsNewAfo}">
<DataGrid.Columns>
<DataGridTextColumn Header="Anzahl">
<DataGridTextColumn.Binding>
<Binding Path="Anzahl" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validation:MaxAnzahlValidationRule>
<validation:MaxAnzahlValidationRule.MaxValueContainer>
<validation:IntegerContainer
DataContext="{Binding
Source={StaticResource Locator},
Path=DataContext}"
Value="{Binding NewJob_VM.CurrentRowValueAnzahl}"
/>
<!--<validation:IntegerContainer Value="{Binding NewJob_VM.CurrentRowValueAnzahl, Mode=TwoWay}" />-->
<!--<validation:IntegerContainer Value="{ Path=CurrentRowValueAnzahl}" />-->
<!--<validation:IntegerContainer Value="{Binding CurrentRowValueAnzahl}" />-->
<!--<validation:IntegerContainer Value="{Binding Path=DataContext.CurrentRowValueAnzahl, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />-->
<!--<validation:NewJobNewAfoRowAnzahlWrapper MaxAnzahl="{Binding Source={StaticResource RowValue_MaxAnzahl}, Path=CurrentRowValueAnzahl}" />-->
</validation:MaxAnzahlValidationRule.MaxValueContainer>
</validation:MaxAnzahlValidationRule>
</Binding.ValidationRules>
</Binding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<DataGridTemplateColumn Header="AFO ComboBox" Width="260">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox IsEditable="False"
ItemsSource="{Binding DataContext.Afo, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
DisplayMemberPath="Arbeitstext"
SelectedValuePath="Afonummer"
SelectedValue="{Binding NeueAfonummer, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding DataContext.SelectedAfo, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" >
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellEditEnding">
<i:InvokeCommandAction Command="{Binding DataContext.CellEditEndingCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
Etwas zum Ablauf:
Wenn ich den Wert in der Textbox editiere und die Zelle verlasse soll die ValidationRule den Wert mit einem Wert aus einer Property vergleichen den ich zuweise wenn ich eine Zeile im DataGrid markiere.
Code aus meinem ViewModel
private WZS_NewJobNewAfo p_selectedNewJobsNewAfo;
public WZS_NewJobNewAfo SelectedNewJobsNewAfo
{
get { return p_selectedNewJobsNewAfo; }
set
{
if (value == p_selectedNewJobsNewAfo) return;
p_selectedNewJobsNewAfo = value;
if (p_selectedNewJobsNewAfo != null)
CurrentRowValueAnzahl = p_selectedNewJobsNewAfo.Anzahl;
RaisePropertyChanged();
}
}
#endregion Properties
private int p_currentRowValueAnzahl;
public int CurrentRowValueAnzahl
{
get { return p_currentRowValueAnzahl; }
set
{
if (value == p_currentRowValueAnzahl) return;
p_currentRowValueAnzahl = value;
RaisePropertyChanged();
}
}
und noch die ValidationRile Klasse
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));
}
public class NewJobNewAfoRowAnzahlWrapper : DependencyObject
{
public static readonly DependencyProperty MaxAnzahlProperty =
DependencyProperty.Register("MaxAnzahl", typeof(int),
typeof(NewJobNewAfoRowAnzahlWrapper), new FrameworkPropertyMetadata(int.MaxValue));
public int MaxAnzahl
{
get { return (int)GetValue(MaxAnzahlProperty); }
set { SetValue(MaxAnzahlProperty, value); }
}
}
public class MaxAnzahlValidationRule : ValidationRule
{
public NewJobNewAfoRowAnzahlWrapper Wrapper { get; set; }
private int _max;
private IntegerContainer maxValueContainer;
public IntegerContainer MaxValueContainer
{
get { return maxValueContainer; }
set { maxValueContainer = value; }
}
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string s = value.ToString();
int dividend = (int)Convert.ChangeType(value, typeof(int));
int divisor = this.MaxValueContainer.Value;
//_max = Wrapper.MaxAnzahl;
return ValidationResult.ValidResult;
}
}
IntegerContainer ist aus einem Beispiel von CodeProject Codeproject
NewJobNewAfoRowAnzahlWrapper ist ist ein Beispiel von Microsoft TechNet. Beide Varianten funtionieren wenn ich einen Statischen Parameter übergebe aber eben nicht wenn ich denn Wert aus der Property mitgebe dann habe ich immer 0 oder eben den int.MaxValue, also liegt es nicht an der ValidationRule Klasse sondern an meiner Bindung zur Property die krieg ich aber irgendwie nicht gebacken?????????????
Kann mir da bitte jemand einen Tipp geben. Danke, Danke und nochmal Danke...
Hi Qt21580,
hast du mal im Ausgabefenster geschaut, ob es irgendwelche DataBinding-Fehler gibt? Für die Fehlersuche empfehle ich auch den Abschnitt Debugging im Artikel [Artikel] MVVM und DataBinding.
Weeks of programming can save you hours of planning