我有一个包含转换器的项目。完成ContinentSelectionChanged方法调用后,会自动调用转换器。如何更改转换器的值?
XAML
<DataGridComboBoxColumn x:Name="DGKontynent" Header="Kontynent" Width="120" CanUserSort="False" SelectedItemBinding="{Binding Kontynent}" >
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<EventSetter Event="SelectionChanged" Handler="ContinentSelectionChanged" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<DataGridComboBoxColumn x:Name="DGKraj" Header="Kraj" Width="120" CanUserSort="False" SelectedValueBinding="{Binding Kraj}" >
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=Kontynent,Converter={StaticResource CountryConverter}}"/>
<Setter Property="SelectedValue" Value="{Binding Path=Kraj}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=Kontynent,Converter={StaticResource CountryConverter}}" />
<Setter Property="SelectedValue" Value="{Binding Path=Kraj}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
C#Converter
public class CountrySingleConverter : IValueConverter
{
Funkcje FK = new Funkcje();
List<string> kraje = new List<string>();
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
{
kraje = FK.kraj_wybierz(value.ToString(), Edycja.dgKraj);
return kraje;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
C#
List<string> l3 = new List<string>();
static string kontynent_tmp;
private void ContinentSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Xceed.Wpf.Toolkit.MessageBox.Show(DGKraj.SelectedValueBinding.);
string kontynent = null;
l4.Clear();
var row = ZdarzeniaDataGrid.GetSelectedRow();
DataGridCell cell = ZdarzeniaDataGrid.GetCell(row, 5);
if (cell != null)
{
ComboBox combo = GetVisualChild<ComboBox>(cell);
if (combo != null)
{
if (combo.SelectedValue != null)
{
kontynent = combo.SelectedValue.ToString();
kontynent_tmp = kontynent;
}
else
{
kontynent = kontynent_tmp;
}
}
}
l3 = FK.kraj_wybierz(kontynent, DGKraj);
DataGridCell cell3 = ZdarzeniaDataGrid.GetCell(row, 6);
if (cell3 != null)
{
ComboBox combo3 = GetVisualChild<ComboBox>(cell3);
if (combo3 != null)
{
combo3.ItemsSource = l3;
}
}
}
答案 0 :(得分:0)
发送到转换器的值始终是您在{Binding}中绑定的值。如果要绑定到其他内容,则必须指定该内容。
e.g。
{Binding Kontynent.Name}