MV-VM WPF:在将 Databound 对象的父对象传递到 IValueConverter 时维护数据绑定的方法?
我正在使用 model-view-viewmodel 我目前有一个包含 3 条数据的类:2 个整数和一个枚举。
它的构造函数如下所示:
//C#
public Outcome(OutcomeEnum outcomeEnum, Int32 acutalOutcomeData, Int32 expectedOutcomeData)
{
m_outcomeEnum = outcomeEnum;
m_actualData = acutalOutcomeData;
m_expectedData = expectedOutcomeData;
}
我有 2 个彼此相邻的 ComboBox,它们已绑定到一个结果对象列表 (List
),我将其用于“实际”和“预期”整数价值观。
这部分代码如下所示:(SelectedItem 和 ItemsSource 是视图模型中的依赖属性)
<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>
我的问题是:我想将对 Outcome 对象的引用传递到我正在使用的 IValueConverter 中使用在之间进行转换,但这似乎不可能使用 IConvertParameter 而是我将被迫使用多重绑定 msdn 帖子位于此处。
我想简化我的方法,因为为相对简单的东西创建多重绑定似乎有点矫枉过正。
我只想对要传递到 IValueConverter 的 Outcome 对象做一件事,即确定 OutcomeEnum 的枚举类型,以便我可以提供预期或实际数据值的正确格式。
有没有更简单的方法让我能够将 Outcome 对象传递到 IValueConverter OutcomeDataToStringConverter
,同时保持与此 Outcome 对象列表的双向绑定?我愿意接受建议。
I'm using model-view-viewmodel I currently have a class with 3 pieces of data: 2 integers and an enumeration.
Its constructor looks like this:
//C#
public Outcome(OutcomeEnum outcomeEnum, Int32 acutalOutcomeData, Int32 expectedOutcomeData)
{
m_outcomeEnum = outcomeEnum;
m_actualData = acutalOutcomeData;
m_expectedData = expectedOutcomeData;
}
I have 2 ComboBoxes next to each other that I have bound to one List of outcome objects (List<Outcome>
) which I use to the "actual" and "expected" integer values.
This section of code looks like this: (The SelectedItem and ItemsSource are dependency properties in the viewmodel )
<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>
My problem is: I'd like to pass a reference to the Outcome object into the IValueConverter that I'm using to convert between, but this appears to not be possible using the IConvertParameter but rather I would be forced to use Multibinding per the msdn post here.
I'd like to simplify my approach as creating a multibinding for something relatively as simple as this seems like overkill.
I'd like to do only one thing with the Outcome object that I seek to pass into IValueConverter, which is to determine the enumeration type of OutcomeEnum so I can provide the proper formatting of the expected or actual data value.
Is there any simpler way for me to be able to pass the Outcome object into the IValueConverter OutcomeDataToStringConverter
while maintaining a two-way binding with this list of Outcome objects? I'm open to suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不绑定到整个
Outcome
对象而不是它的ActualOutcomeData
或ExpectedOutcomeData
属性?这样转换器将接收整个Outcome
对象,并根据它和转换器参数返回正确的值。Why don't you bind to the entire
Outcome
object instead of itsActualOutcomeData
orExpectedOutcomeData
properties? That way the converter will receive the entireOutcome
object, and based on it and on the converter parameter return the proper value.现在,我最终更改了 Outcome 类,因此它只有一个成员 -outcomeData
然后我创建了两个绑定到组合框的 ItemsSource 的 Outcome 对象列表 - ActualOutcomeList 和 ExpectedOutcomeList。
然后,我可以将 Outcome 对象本身绑定到组合框,但通过使用两个列表,我可以避免我在 Avaid 帖子的评论中描述的重复选择问题。
最后代码如下所示:
For right now, I ended up changing the Outcome class so it had only one member - outcomeData
Then I created two lists of Outcome objects which were bound to the combobox's ItemsSource - ActualOutcomeList and ExpectedOutcomeList.
I can then bind the Outcome object itself to the Combobox, but by having two lists I avoid the duplicate selection issue I describe in the comment on Avaid's post.
In the end the code looks like this: