WPF:如何将变量后面的代码中的数据绑定到xaml中的内容

时间:2017-03-27 19:24:50

标签: c# wpf xaml data-binding

我无法从我的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"/>

MainWindow.xaml.cs

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Produkte produkte = new Produkte();
        produkte.Name1 = "Handstaubsauger";
        produkte.Name2 = "Fensterwascher";
        produkte.Name3 = "Dampfreiniger";
        produkte.Name4 = "Hochdruckreiniger";
        produkte.Name5 = "Geschenkgutschein";

        // 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

    }

}

Produkte.cs

public class Produkte
{
    public String Name1 { get; set; }
    public String Name2 { get; set; }
    public String Name3 { get; set; }
    public String Name4 { get; set; }
    public String Name5 { get; set; }
    public Int16 Stimmen1;
    public Int16 Stimmen2;
    public Int16 Stimmen3;
    public Int16 Stimmen4;
    public Int16 Stimmen5;
}

2 个答案:

答案 0 :(得分:0)

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
}

请注意,如果您尝试在视图中绑定到Stimmen1Stimmen2等,则会失败,因为它们是字段。他们必须{ get; }才能绑定它们。

您确实需要将Produkte转换为具有INotifyPropertyChanged等的正确视图模型,但这将获得一个只读视图。

答案 1 :(得分:-2)

在顶部Window标记内的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}}">

您需要在代码中公开Product类型的公共属性:

    public Product Product
    {
        get { return new Product() {Id = 1, Name = "James"}; }
    }

然后在xaml中,你会这样做:

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