0
在DataGridTemplateColumn DataTemplate中,我想將2個控件綁定到格式爲「[name]:[value]」的字符串字段,即該字符串由冒號「:」分隔。我需要將控件a)綁定到[名稱]部分,並控制b)值部分。如何將2個控件綁定到1個字段並訪問ConvertBack的2個控件值?
我已經能夠成功地使用的IValueConverter來拆分顯示的字符串:
public class NameAndValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string rtn = "";
string[] split = value.ToString().Split(':');
if (split.Count() == 2)
{
if(parameter.ToString() == "Name")
rtn = split[0];
if(parameter.ToString() == "Value")
rtn = split[1];
}
return rtn;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new InvalidOperationException("NameAndValueConverter can only be used OneWay.");
}
}
而XAML:
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<local:NameAndValueConverter x:Key="NameAndValueConverter" />
</StackPanel.Resources>
<TextBox x:Name="namePart" Text="{Binding Path=FieldType, Converter={StaticResource NameAndValueConverter}, ConverterParameter='Name'}" />
<TextBox x:Name="valuePart" Text="{Binding Path=FieldType, Converter={StaticResource NameAndValueConverter}, ConverterParameter='Value'}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
但數據可以在文本框編輯怎麼能我訪問ConvertBack中的2個TextBox值,以便它們可以再次連接?
你有訪問你綁定的視圖模型?你能介紹2個新的屬性到該視圖模型? – Andy