M-V-VM WPF:的方式来维持数据绑定,同时通过父母对象的数据绑定对象转IValueConverter?
-
21-09-2019 - |
题
我使用的模型视图模型我现在有一类有3件数据:2整数和一枚举。
它的构造是这样的:
//C#
public Outcome(OutcomeEnum outcomeEnum, Int32 acutalOutcomeData, Int32 expectedOutcomeData)
{
m_outcomeEnum = outcomeEnum;
m_actualData = acutalOutcomeData;
m_expectedData = expectedOutcomeData;
}
我有2个组合框彼此相邻,我已经开到一个列表中的结果的对象(List<Outcome>
),我使用的"实际"和"预期"整数值。
本节的代码看起来是这样的:(该SelectedItem和该视图可以是依赖性的视图模型)
<ComboBox ItemsSource="{Binding Path=OutcomeList}" SelectedItem="{Binding SelectedExpectedOutcome, Mode=TwoWay}" x:Name="PART_cbExpectedOutcome" Grid.Column="1" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Path=ExpectedOutcomeData, Converter={StaticResource OutcomeDataToStringConverter}, ConverterParameter=Expected }" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox ItemsSource="{Binding Path=OutcomeList}" SelectedItem="{Binding SelectedActualOutcome, Mode=TwoWay}" x:Name="PART_cbActualOutcome" Grid.Column="2" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Path=ActualOutcomeData, Converter={StaticResource OutcomeDataToStringConverter}, ConverterParameter=Actual}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我的问题是: 我想通过一个参考成果的对象入IValueConverter,我使用之间的转换,但这似乎是不可能的使用IConvertParameter而我将被迫使用Multibinding每 msdn后在这里.
我想我简化方法,因为创建一个multibinding东西比较简单,因为这似乎是大材小用。
我想做的只有一件事有成果的对象,我寻求通过入IValueConverter,这是确定列举的类型OutcomeEnum所以我可以提供的适当格式的预期或实际数据的价值。
是否有任何简单的办法让我能够通过成果的对象入IValueConverter OutcomeDataToStringConverter
在保持双方结合与这个列表中的结果的对象?我是开放的建议。
解决方案 2
现在,我结束了改变的结果类,因此它只有一个成员outcomeData
然后我创建了两个列表中的结果的对象是结组合框的该视图可以-ActualOutcomeList和ExpectedOutcomeList.
然后我就可以结合成果的对象本身的组合框,而是通过具有两个列表,避免重复的选择问题,我描述了在评论Avaid的员额。
在结束码看起来是这样的:
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Converter={StaticResource OutcomeToStringConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox
x:Name="PART_cbActualOutcome"
Grid.Column="2"
ItemsSource="{Binding Path=ActualOutcomeList}"
SelectedItem="{Binding SelectedActualOutcome, Mode=TwoWay}"
IsEnabled="{Binding Path=IsOutcomeEnabled}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Converter={StaticResource OutcomeToStringConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
其他提示
你为什么不绑到整个 Outcome
对象,而不是它的 ActualOutcomeData
或 ExpectedOutcomeData
性?这种方式转换器将收到整个 Outcome
对象,并在此基础上,并在转换参数返回的适当价值。
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Converter={StaticResource OutcomeToStringConverter}, ConverterParameter=Expected}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>