Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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# 用户控件中的WPF命令绑定不起作用_C#_Wpf_Xaml_Data Binding - Fatal编程技术网

C# 用户控件中的WPF命令绑定不起作用

C# 用户控件中的WPF命令绑定不起作用,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我自己搜索网页,并通过下面的例子,试图找到一个解决方案,但迄今为止我所做的一切都失败了。我知道我在WPF方面的糟糕经验让我错过了一些巨大而愚蠢的东西,但事实上我被卡住了。 正如在对象中所写的那样,我有一个自定义UserControl,其中包含一个单选按钮。我想通过我的UserControl的DependencyProperty将单选按钮的命令暴露在外部。 UserControl(名为“ImageRadioButton”)的.xaml如下所示: <UserControl x:Class=&q

我自己搜索网页,并通过下面的例子,试图找到一个解决方案,但迄今为止我所做的一切都失败了。我知道我在WPF方面的糟糕经验让我错过了一些巨大而愚蠢的东西,但事实上我被卡住了。 正如在对象中所写的那样,我有一个自定义UserControl,其中包含一个
单选按钮
。我想通过我的UserControl的DependencyProperty将
单选按钮的
命令
暴露在外部。 UserControl(名为“ImageRadioButton”)的.xaml如下所示:

<UserControl x:Class="WpfSinergoHMIControls.Controlli.ImageRadioButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"                           
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">

    <UserControl.Resources>

    </UserControl.Resources>

    <Grid>
        <Grid>
            <RadioButton Command="{Binding SomeCommand, ElementName=me}"  Name="button1" Foreground="White">

            </RadioButton>
        </Grid>   
    </Grid>
</UserControl>
public static readonly DependencyProperty SomeCommandProperty =
DependencyProperty.Register(
   "SomeCommand",
   typeof(ICommand),
   typeof(ImageRadioButton),
   new UIPropertyMetadata(null));

public ICommand SomeCommand
{
  get { return (ICommand)GetValue(SomeCommandProperty); }
  set { SetValue(SomeCommandProperty, value); }
}
最后,我在使用我的UserControl的应用程序中声明一个距离:

<Controlli:ImageRadioButton x:Name="btnAutomatic" GroupName="MainMenu" SomeCommand="{Binding DataContext.NavigateAutomaticCommand, ElementName=MainViewObj}" HorizontalAlignment="Left" Height="60" VerticalAlignment="Bottom" Width="140" Canvas.Left="1373" Canvas.Top="5" Margin="6,0,0,5" IsChecked="True"/>

毫无意义地说这不起作用(没有调用任何命令)。我知道我遗漏了一些愚蠢的东西,但经过多次尝试/搜索,我仍然找不到解决方案。
谢谢

您在命令绑定中引用了元素
me
,但没有在任何地方指定该名称,这意味着在运行时找不到绑定源(您的
UserControl

Command="{Binding SomeCommand, ElementName=me}"
如果您在
UserControl
上设置了名称,那么一切都会按预期工作(至少对我来说是这样)


您应该在UserControl上添加x:Name=“me”。
<UserControl x:Class="WpfSinergoHMIControls.Controlli.ImageRadioButton"
             ...
             x:Name="me">