我尝试从DataGrid中选定值绑定并显示到上TextBlock的用户。然后将其绑定到变量在所述源代码中的模型
这是一个有点示例代码,使其更容易在这里显示的问题。
我的XAML文件:
<Window x:Class="WpfAppTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<DataGrid x:Name="DGExample" MinWidth="50" SelectionMode="Single" FontSize="30"
ItemsSource="{Binding ExampleList, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
</DataGrid>
<TextBlock Name="TBDescription" MinWidth="100" FontSize="30">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="Example" Mode="OneWayToSource" UpdateSourceTrigger="PropertyChanged" />
<Binding Path="SelectedItem.X" ElementName="DGExample" Mode="OneWay" UpdateSourceTrigger="PropertyChanged" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Name="ShowExample" FontSize="30" Text="{Binding Path=Example, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
</TextBlock>
</StackPanel>
</Grid>
</Window>
我的模型例如:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfAppTest
{
public class ModelExample : INotifyPropertyChanged
{
public int Example
{
get;
set;
}
public ObservableCollection<ClassExample> ExampleList { get; set; }
public ModelExample()
{
ExampleList = new ObservableCollection<ClassExample>() {
new ClassExample(1,2), new ClassExample(3,4), new ClassExample(5,6)};
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
我的例子类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfAppTest
{
public class ClassExample
{
public ClassExample(int x, int y)
{
X = x;
Y = y;
}
public int X{get;set;}
public int Y{ get; set; }
}
}
我的鸟文件:
<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<PropertyChanged />
</Weavers>
我希望看到在TBDescription控制的值的举例变量
但是存储在示例中唯一值仅为0 ...
与您的代码的问题是,它从来没有结合的价值,因为它的内容是不是永远改变从Example
TextBlock
财产。所以,如果你只是想绑定DataGrid
的设定值,以Example
做到以下几点。创建转换器,其ClassExample
实例int
public class TestConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
ClassExample obj = value as ClassExample;
if (obj != null)
{
return obj.X;
}
return 0;
}
}
添加转换器Window
<Window.Resources>
<your-namespace:TestConverter x:Key="TestConverter" />
</Window.Resources>
添加绑定在SelectedItem
到DataGrid
<DataGrid
x:Name="DGExample"
MinWidth="50"
SelectionMode="Single"
FontSize="30"
ItemsSource="{Binding ExampleList, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Example, Mode=OneWayToSource, Converter={StaticResource TestConverter}}">