Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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:如何将代码隐藏变量中的数据绑定到xaml中的内容_C#_Wpf_Xaml_Data Binding - Fatal编程技术网

C# WPF:如何将代码隐藏变量中的数据绑定到xaml中的内容

C# WPF:如何将代码隐藏变量中的数据绑定到xaml中的内容,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我无法从wpf单选按钮中的代码隐藏绑定变量 有人能帮我显示单选按钮内容中变量的值吗 MainWindow.xaml: <RadioButton GroupName="Preis" Grid.Row="10" Content="{Binding Name1}" FlowDirection="RightToLeft" HorizontalAlignment="Left"/> <RadioButton GroupName="Preis

我无法从wpf单选按钮中的代码隐藏绑定变量

有人能帮我显示单选按钮内容中变量的值吗

MainWindow.xaml:

            <RadioButton GroupName="Preis" Grid.Row="10"  Content="{Binding Name1}" FlowDirection="RightToLeft" HorizontalAlignment="Left"/>
            <RadioButton GroupName="Preis" Grid.Row="11" Content="{Binding Name2}" FlowDirection="RightToLeft" HorizontalAlignment="Left"/>
            <RadioButton GroupName="Preis" Grid.Row="12" Content="{Binding Name3}" FlowDirection="RightToLeft" HorizontalAlignment="Left"/>
            <RadioButton GroupName="Preis" Grid.Row="13" Content="{Binding Name4}" FlowDirection="RightToLeft" HorizontalAlignment="Left"/>
            <RadioButton GroupName="Preis" Grid.Row="14" Content="{Binding Name5}" FlowDirection="RightToLeft" HorizontalAlignment="Left"/>

在顶部窗口标记内的xaml中,需要定义
DataContext
。如果它是你的代码,它将是
Self
。一旦设置了
DataContext
,就可以访问公共属性进行绑定

<Window x:Class="Account.Client.PotentialMisallocation.Controls.DisplayAndFilter"
         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"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">
然后在xaml中,您将执行以下操作:

 <Label x:Name="label" Content="{Binding Product.Id}" />
 <Label x:Name="label1" Content="{Binding Product.Name}" />

公共主窗口()
{
初始化组件();
Produkte Produkte=新Produkte();
produkte.Name1=“Handstaubsauger”;
produkte.Name2=“Fensterwascher”;
produkte.Name3=“Dampfreiniger”;
produkte.Name4=“Hochdruckreiniger”;
produkte.Name5=“Geschenkgutschein”;
//加上这个
DataContext=produkte;
//Regex für电子邮件

字符串regexEmail=@“^(?“”(“”)+?(?我必须如何调整xaml中的内容?它仍然不显示文本。我是否提出了错误的建议?Downvoter,请你发表评论好吗?你为什么要告诉他将DataContext设置为self?这完全没有必要,而且会成为一个非常坏的习惯。他们使用用户控件进行操作,这会导致无尽的挫折。分配viewmodel到DataContext。这就是DataContext的用途。我会高度自信地猜测这是dv背后的原因。不过,OP特别要求参考代码背后。我确实理解MVVM的概念,但OP似乎不知道,甚至可能不需要他的场景。无论如何,感谢您的澄清。如果OP当他问这个问题时,他不知道他问的是一个稍微错误的问题,或者更准确地说是以一种稍微错误的方式思考正确的问题。他没有充分的理由去做错事。他只是问如何做,以便他能看到屏幕上的字符串。
<Window x:Class="Account.Client.PotentialMisallocation.Controls.DisplayAndFilter"
         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"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">
    public Product Product
    {
        get { return new Product() {Id = 1, Name = "James"}; }
    }
 <Label x:Name="label" Content="{Binding Product.Id}" />
 <Label x:Name="label1" Content="{Binding Product.Name}" />
public MainWindow()
{
    InitializeComponent();

    Produkte produkte = new Produkte();
    produkte.Name1 = "Handstaubsauger";
    produkte.Name2 = "Fensterwascher";
    produkte.Name3 = "Dampfreiniger";
    produkte.Name4 = "Hochdruckreiniger";
    produkte.Name5 = "Geschenkgutschein";


    //  ADD THIS
    DataContext = produkte;


    // Regex für Email
    String regexEmail = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";

    // Hier weitermachen
}