C# 组合框内的按钮,如何传递;SelectedItem";作为按钮的命令参数的Combobox?

C# 组合框内的按钮,如何传递;SelectedItem";作为按钮的命令参数的Combobox?,c#,wpf,devexpress,commandbinding,C#,Wpf,Devexpress,Commandbinding,我无法访问位于按钮内的组合框的SelectedItem。我想将SelectedItem作为按钮的CommandParameter传递给我的虚拟机。在我的虚拟机中,我使用MVVMLight的ICommand 我做错了什么 <dx:SimpleButton Margin="0,5,0,5" MinWidth="160" Command="{Binding CreateNewSymbolCommand}" CommandParameter="{Binding ElementName=AssetC

我无法访问位于按钮内的组合框的
SelectedItem
。我想将SelectedItem作为按钮的
CommandParameter
传递给我的虚拟机。在我的虚拟机中,我使用MVVMLight的
ICommand

我做错了什么

<dx:SimpleButton Margin="0,5,0,5" MinWidth="160" Command="{Binding CreateNewSymbolCommand}" CommandParameter="{Binding ElementName=AssetClassInButton, Path=SelectedItem}">
            <dx:SimpleButton.ContentTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBox Text="Choose Asset Class" Foreground="LightGreen" HorizontalAlignment="Center"/>
                        <dxe:ComboBoxEdit Name="AssetClassInButton" MinWidth="150" IsTextEditable="False" ItemsSource="{Binding Source={StaticResource AssetClassEnumValues}}"/>
                    </StackPanel>

                </DataTemplate>
            </dx:SimpleButton.ContentTemplate>
        </dx:SimpleButton>

摆脱ContentTemplate\DataTemplate-您不需要它,因为您直接设置按钮的内容,而不是重复出现的元素的模板

<dx:SimpleButton Margin="0,5,0,5" MinWidth="160" Command="{Binding CreateNewSymbolCommand}" CommandParameter="{Binding ElementName=AssetClassInButton, Path=SelectedItem}">
    <StackPanel Orientation="Vertical">
        <TextBox Text="Choose Asset Class" Foreground="LightGreen" HorizontalAlignment="Center"/>
        <dxe:ComboBoxEdit Name="AssetClassInButton" MinWidth="150" IsTextEditable="False" ItemsSource="{Binding Source={StaticResource AssetClassEnumValues}}"/>
    </StackPanel>
</dx:SimpleButton>


您是否尝试绑定到SelectedValue?我认为绑定到SelectedItem将为您提供一个框架element@MartinGrundy没有运气。我怀疑button commandparameter无法获取随后定义的元素的elementname?或者我必须在命令中使用相对资源?卡住了…在命令上使用相对源参数biding值得一试。尽管直接使用元素名通常是可以的。调试绑定解析错误时,输出窗口中是否出现错误?这通常是我找到任何问题答案的地方,我得到“找不到引用'ElementName=AssetClassInButton'绑定的源”。BindingExpression:Path=SelectedItem;DataItem=null;目标元素是'SimpleButton'(名称='');目标属性是'CommandParameter'(类型'Object')”。恐怕我不知道如何将绑定指向位于按钮自己的内容模板中的孩子。太好了,这是开箱即用的。只是想知道为什么在模板中找不到元素。访问元素模板中的子元素时是否存在问题?使用内容模板时,通常会在不同的模板之间切换,或者使用它们重新生成元素(例如ItemsControl)。可视化树中的父级控件无法轻松访问这些控件,因为它们不在其中(例如,如果您关闭模板,或者一个ItemsControl可能有多个实例)。从模板开始,如果需要的话,使用RelativeSource在可视化树上行走会更早,我还没有这样做的必要。-希望这有帮助?谢谢你的解释。我不需要这个特定问题的模板,因此您解决了我的问题。谢谢如果我将来需要通过templateselector切换模板,那么我将提出一个新问题。