【问题标题】:WPF: How to bind data from a code behind variable to content in xamlWPF:如何将代码后面的变量中的数据绑定到 xaml 中的内容
【发布时间】:2017-03-27 19:24:49
【问题描述】:

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

    }

}

产品.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;
}

【问题讨论】:

    标签: c# wpf xaml data-binding


    【解决方案1】:
    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 等的正确视图模型,但这将使只读视图现在正常工作。

    【讨论】:

      【解决方案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}" />
      

      【讨论】:

      • 如何调整 xaml 中的内容?它仍然不显示文本。
      • 我说错了吗? Downvoter,你能发表评论吗?
      • 你为什么要告诉他将DataContext设置为self?这是完全没有必要的,而且会成为一个非常糟糕的习惯。他们使用 UserControls 来做这件事,这会导致无穷无尽的挫败感。将 viewmodel 分配给 DataContext。这就是 DataContext 的用途。我很有把握地猜测这就是 dv 的背后原因。
      • OP 专门要求参考后面的代码。我确实了解 MVVM 的概念,但 OP 似乎不知道,甚至可能不需要他的场景。无论如何,感谢您的澄清。
      • 如果 OP 在问这个问题,他不知道他在问一个稍微错误的问题,或者更准确地说是以稍微错误的方式思考正确的问题。他没有充分的理由做错事。他只是在问如何制作它,以便他可以看到屏幕上的琴弦。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 2012-07-15
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多