您必须在绑定表达式中为索引器的参数设置适当的类型。
查看型号:
public enum Property
{
PrimaryAddress,
SecondaryAddress,
UsePrimaryAddress
}
public class ViewModel
{
public ViewModel()
{
Properties = new Dictionary<Property, object>
{
{ Property.PrimaryAddress, "123" },
{ Property.SecondaryAddress, "456" },
{ Property.UsePrimaryAddress, true }
};
}
public Dictionary<Property, object> Properties { get; private set; }
}
XAML:
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication5"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="{Binding Path=Properties[(local:Property)PrimaryAddress]}"/>
<TextBox Grid.Row="1" Text="{Binding Path=Properties[(local:Property)SecondaryAddress]}"/>
<CheckBox Grid.Row="2" IsChecked="{Binding Path=Properties[(local:Property)UsePrimaryAddress]}"/>
</Grid>
</Window>
代码隐藏:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
有关详细信息,请参阅“绑定路径语法”。