在我的wpf项目中,我有一个数据网格,它由数据集填充,包含一些列和许多行。我想迭代Column [1] Rows [i](例如,获取数据网格中所有行的列[1]的单元格内的值)。 我的问题是 如何将这些单元格值绑定到单个文本框? 我知道使用多重绑定将是实现解决方案的方法之一但我没有找到任何帮助关于通过数据网格多重绑定文本框。例如,我已阅读以下问题:
How to bind multiple values to a single WPF TextBlock?
How to use a MultiBinding on DataGridTextColumn?
此外,绑定单个值是可以实现的,我已经这样做了。 我将不胜感激任何帮助。在此先感谢!!
我的XAML:
<DataGrid x:Name="datagridbatch"
FontSize="13.333" FontWeight="Normal"
IsReadOnly="True"
SelectionChanged="datagridbatch_SelectionChanged"
SelectionUnit="FullRow" SelectionMode="Single"
VerticalAlignment="Top" HorizontalAlignment="Right"
Height="615" Width="373" Margin="0,0,0,-582"
CanUserResizeColumns="False" CanUserResizeRows="False"
CanUserDeleteRows="False" CanUserAddRows="False"
RowHeight="30"
Grid.Row="5" Grid.Column="1"
CanUserReorderColumns="False" CanUserSortColumns="False"
ColumnHeaderHeight="25" ColumnWidth="*"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto" />
<TextBox x:Name="input2"
Margin="0,0,0,0" Width="490" Height="30"
Grid.Row="0" Grid.Column="1"
HorizontalAlignment="Left"
Background="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"
FontSize="13.333" FontWeight="Normal"
Text="{Binding SelectedItem.UNIQUEPART_ID, ElementName=datagridbatch}"
BorderBrush="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"
FontFamily="Tahoma"
IsReadOnlyCaretVisible="True"
HorizontalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True"/>
答案 0 :(得分:0)
这个想法是使用普通的Binding
和一个处理项目集合的转换器。使用MultiBinding
并不能真正解决绑定源项的动态集合问题。所以需要什么:
DataGrid
,其中每个项目都包含特定属性TextBox
Binding
属性上的TextBox.Text
,其中SelectedItems
与DataGrid
绑定转换器以创建单个文本Converter
让我们从xaml开始,它可以非常简单:
<Window.Resources>
<local:ItemsToTextConverter x:Key="cItemsToTextConverter"/>
</Window.Resources>
<!-- your surrounding controls -->
<DataGrid x:Name="datagridbatch" SelectionChanged="datagridbatch_SelectionChanged"/>
<TextBox x:Name="input2" Text="{Binding ElementName=datagridbatch,Path=SelectedItems,Converter={StaticResource cItemsToTextConverter},Mode=OneWay}"/>
请注意,绑定只能以一种方式运行 - 将字符串值分配回多个项目并不像将项目压缩为单个字符串那样容易。
转换器需要获取项集合并从属性中提取字符串:
public class ItemsToTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var items = value as IEnumerable;
if (items != null)
{
// note: items may contain the InsertRow item, which is of different type than the existing items.
// so the items collection needs to be filtered for existing items before casting and reading the property
var items2 = items.Cast<object>();
var items3 = items2.Where(x => x is MyItemType).Cast<MyItemType>();
return string.Join(Environment.NewLine, items3.Select(x => x.UNIQUEPART_ID));
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new InvalidOperationException();
}
}
此外,当选择更改时,DataGrid.SelectedItems
不会自动触发绑定更新,因此您需要在选择更改事件处理程序中触发手动更新:
void datagridbatch_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var binding = BindingOperations.GetBindingExpression(input2, TextBox.TextProperty);
if (binding != null)
{
binding.UpdateTarget();
}
}