c#| WPF中使用变量的不同调用

c#| WPF中使用变量的不同调用,c#,wpf,C#,Wpf,现在我有多个按钮和其他元素。每一个都有一个x:Name和一个Content 我还有一个列表,其中包含与WPF部件的x:Name同名的元素 我的目标是拥有一个可以反复调用的函数,给它一个元素的x:name,并将列表中名称({name1,value,name2,value,name3,value..})后的值设置为具有该名称的WPF元素的内容 例如: 我有一个列表: 配置{树,1,椅子,4,窗户,3,美元,200} 和WPF要素: <Button x:Name="chair" Content

现在我有多个按钮和其他元素。每一个都有一个x:Name和一个Content

我还有一个列表,其中包含与WPF部件的x:Name同名的元素

我的目标是拥有一个可以反复调用的函数,给它一个元素的x:name,并将列表中名称({name1,value,name2,value,name3,value..})后的值设置为具有该名称的WPF元素的内容

例如:

我有一个列表:
配置{树,1,椅子,4,窗户,3,美元,200}

和WPF要素:

<Button x:Name="chair" Content="12"/> 
<Button x:Name="tree" Content="5"/>

现在我想调用一个函数,给它config{}和x:Name=“chair”。 它现在应该在配置中搜索元素“chair”,然后用内容中的12替换后面的4。 现在我想用next按钮(x:Name=“tree”)调用它,将值1更改为5

我真的不知道,如何恰当地描述它:(

这就是我现在拥有的功能:

 public void SetConfig(List<string> config, string name)
        {
            config[config.IndexOf(name) + 1] = myWindow.name.Content.ToString();

        }
public void SetConfig(列表配置,字符串名称)
{
config[config.IndexOf(name)+1]=myWindow.name.Content.ToString();
}
myWindow.NAME.Content.ToString()部分不起作用,因为我不能在其中使用变量

有没有办法,我还能这样做


提前感谢:D

您可以通过从绑定到多个按钮的单击事件中强制转换发件人来访问按钮属性。如下所示:

    private void Click(object sender, RoutedEventArgs e)
    {
        Title = ((Button) sender).Name;
    }

这个例子对我很有用。你必须在WPF表单上给你的容器命名(例如:网格), 并使用方法“FindName(string)”,如下所示:

XAML:


代码隐藏:

public partial class MainWindow : Window
    {

        List<string> config = new List<string>() { "tree", "1", "chair", "4", "window", "3", "dollar", "200" };

        public MainWindow()
        {
            InitializeComponent();
        }

        private void tree_Click(object sender, RoutedEventArgs e)
        {
            SetConfig(config, this.tree.Name);
        }

        private void chair_Click(object sender, RoutedEventArgs e)
        {
            SetConfig(config, this.chair.Name);
        }

        public void SetConfig(List<string> config, string name)
        {
            config[config.IndexOf(name) + 1] = ((Button)LayoutRoot.FindName(name)).Content.ToString();

        }       
    }
公共部分类主窗口:窗口
{
列表配置=新列表(){“树”、“1”、“椅子”、“4”、“窗口”、“3”、“美元”、“200”};
公共主窗口()
{
初始化组件();
}
私有无效树\u单击(对象发送者,路由目标)
{
SetConfig(config,this.tree.Name);
}
专用无效椅子单击(对象发送者,路由目标e)
{
SetConfig(config,this.chair.Name);
}
public void SetConfig(列表配置,字符串名称)
{
config[config.IndexOf(name)+1]=((按钮)LayoutRoot.FindName(name)).Content.ToString();
}       
}
注意:如果您使用MVVM模式,这是不可用的


希望有帮助。

看起来您使用的是
列表
,就像它是
键值对
提供者一样,请尝试用
词典替换您的
列表

您还可以在
代码隐藏
中使用一种方法来管理所有
按钮
OnClick事件
,这样您就不必为每个额外的
按钮
添加新的
OnClick
处理程序

main window.xaml

<Window x:Class="SO.Q_27171497.Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Button x:Name="chair" Content="12" Click="Button_OnClick"/>
        <Button x:Name="tree" Content="5" Click="Button_OnClick"/>
    </StackPanel>
</Window>

MainWindow.xaml.cs

namespace SO.Q_27171497.Wpf
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;

    public partial class MainWindow
    {
        private readonly Dictionary<string, int> config = new Dictionary<string, int>
        {
            {"tree", 1},
            {"chair", 4},
            {"window", 3},
            {"dollar", 200}
        };

        public MainWindow()
        {
            this.InitializeComponent();
        }

        private void Button_OnClick(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            if (button == null)
            {
                return;
            }
            button.Content = this.config.FirstOrDefault(c => String.CompareOrdinal(button.Name, c.Key) == 0).Value;
        }
    }
}
namespace SO.Q_27171497.Wpf
{
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows;
使用System.Windows.Controls;
公共部分类主窗口
{
专用只读字典配置=新建字典
{
{“树”,1},
{“主席”,4},
{“窗口”,3},
{“美元”,200}
};
公共主窗口()
{
this.InitializeComponent();
}
私有无效按钮\u OnClick(对象发送器,路由目标)
{
var按钮=发送方为按钮;
如果(按钮==null)
{
回来
}
button.Content=this.config.FirstOrDefault(c=>String.CompareOrdinal(button.Name,c.Key)==0);
}
}
}

您可以尝试使用中的答案访问控件。这为我解决了问题。非常感谢!我没有像您那样创建网格,而是使用((按钮)myWindow.FindName(名称)).Content.ToString();
namespace SO.Q_27171497.Wpf
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;

    public partial class MainWindow
    {
        private readonly Dictionary<string, int> config = new Dictionary<string, int>
        {
            {"tree", 1},
            {"chair", 4},
            {"window", 3},
            {"dollar", 200}
        };

        public MainWindow()
        {
            this.InitializeComponent();
        }

        private void Button_OnClick(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            if (button == null)
            {
                return;
            }
            button.Content = this.config.FirstOrDefault(c => String.CompareOrdinal(button.Name, c.Key) == 0).Value;
        }
    }
}