Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Textblock绑定到字典值,其中键由用户输入给定_C#_Wpf_Dictionary_Data Binding - Fatal编程技术网

C# 将Textblock绑定到字典值,其中键由用户输入给定

C# 将Textblock绑定到字典值,其中键由用户输入给定,c#,wpf,dictionary,data-binding,C#,Wpf,Dictionary,Data Binding,以上是我想要得到的行为。工具中的值应该用作访问存储价格的词典的键。 假设我们有一些货币集合,它们显示在项目控件中,如下所示: <ItemsControl Background="White" Name="currenciesList" ItemsSource="{Binding Currencies}"> <ComboBox Name="cmbbx" Height="20" Width="60" Grid.Column="1" ItemsSource="{Bin

以上是我想要得到的行为。工具中的值应该用作访问存储价格的词典的键。 假设我们有一些货币集合,它们显示在项目控件中,如下所示:

<ItemsControl Background="White" Name="currenciesList" ItemsSource="{Binding Currencies}">
<ComboBox Name="cmbbx" Height="20" Width="60" Grid.Column="1" 
      ItemsSource="{Binding Path=InstrumentTypes}" 
      SelectedValue="{Binding CurrencyInstrument}"  
      IsSynchronizedWithCurrentItem="False" DisplayMemberPath="Key" SelectedValuePath="Value"/>

<TextBlock Name="purchase_price" 
       Grid.Column="2" 
       Text="{Binding CurrencyInstrument.PurchasePrice}"/>

<TextBlock Name="sale_price" 
       Grid.Column="3" 
       Text="{CurrencyInstrument.SalePrice}"/>

我们定义了一个datatemplate来描述如何使用以下属性显示列表中的每种货币:

<ComboBox Name="cmbbx" Height="20" Width="60" Grid.Column="1" ItemsSource="{Binding Path=InstrumentTypes}" SelectedItem="{Binding CurrencyInstrument}"  IsSynchronizedWithCurrentItem="False">
</ComboBox>
<TextBlock Name="purchase_price" Grid.Column="2" Text="{Binding What do I put here??}">
</TextBlock>
<TextBlock Name="sale_price" Grid.Column="3" Text="{Binding What do I put here??}"/>

对于textblock绑定,我希望有一个类似PurchaseInstruments[CurrencyInstrument]的表达式,其中PurchaseInstruments是字典属性,CurrencyInstrument是另一个存储要在字典中使用的键值的属性。我如何实现这种行为


请忽略图像中糟糕的格式设置。

字典本质上是KeyValuePair类型,因此您的XAML应该如下所示:

<ItemsControl Background="White" Name="currenciesList" ItemsSource="{Binding Currencies}">
<ComboBox Name="cmbbx" Height="20" Width="60" Grid.Column="1" 
      ItemsSource="{Binding Path=InstrumentTypes}" 
      SelectedValue="{Binding CurrencyInstrument}"  
      IsSynchronizedWithCurrentItem="False" DisplayMemberPath="Key" SelectedValuePath="Value"/>

<TextBlock Name="purchase_price" 
       Grid.Column="2" 
       Text="{Binding CurrencyInstrument.PurchasePrice}"/>

<TextBlock Name="sale_price" 
       Grid.Column="3" 
       Text="{CurrencyInstrument.SalePrice}"/>

通过将文本框直接绑定到组合框中的选定值,解决了此问题。关键的洞察是将购买和销售价格封装在CurrencyInstrument对象中,以便我们可以在文本框中绑定时直接访问属性。下面是XAML代码

<ComboBox Name="cmbbx" Height="20" Width="60" Grid.Column="1" SelectedIndex="0" ItemsSource="{Binding Path=InstrumentTypes}" DisplayMemberPath="Key" SelectedValuePath="Value" IsSynchronizedWithCurrentItem="False"></ComboBox>
<TextBlock Name="purchase_price" Grid.Column="2" Text="{Binding ElementName=cmbbx, Path=SelectedValue.PurchasePrice}"/>
<TextBlock Grid.Column="3" Text="{Binding ElementName=cmbbx, Path=SelectedValue.SalePrice}"/>

感谢您的回复,它启发我重构我的设计,将CurrencyInstrument表示为一个具有购买价格和销售价格的对象,而不是字典中的键值对。现在我的问题是,我当前正在将SelectedValue绑定到ViewModel中的一个变量,但这会导致所有货币的工具类型都发生更改。但是,我希望每种货币都能够独立地进行更改。我是否需要维护与每种货币对应的此类变量列表?