C# ItemControl:根据“更改ItemControl中控件的属性”;内容“;控制

C# ItemControl:根据“更改ItemControl中控件的属性”;内容“;控制,c#,.net,wpf,xaml,itemscontrol,C#,.net,Wpf,Xaml,Itemscontrol,我有以下代码 在我的示例中,我使用ItemControl显示按钮。 现在,我需要根据其内容访问特定按钮 改变它的性质 例如,当单击ChangeBackGround按钮时,我需要更改first按钮的Background属性。[请参见下面我的问题的屏幕截图] 查看以下代码和屏幕截图: public partial class MainWindow : Window { private ObservableCollection<Data> dataCollectio

我有以下代码

在我的示例中,我使用
ItemControl
显示按钮。 现在,我需要根据其内容访问特定按钮 改变它的性质

例如,当单击
ChangeBackGround
按钮时,我需要更改
first
按钮的
Background
属性。[请参见下面我的问题的屏幕截图]

查看以下代码和屏幕截图:

public partial class MainWindow : Window
    {
        private ObservableCollection<Data> dataCollection ;

        public MainWindow()
        {
            InitializeComponent();
            dataCollection = new ObservableCollection<Data>();

            dataCollection.Add(new Data("first"));
            dataCollection.Add(new Data("second"));
            dataCollection.Add(new Data("third"));
            dataCollection.Add(new Data("forth"));
            this.DataContext = this;
        }

        public ObservableCollection<Data> DataCollection
        {
            get
            {
                return this.dataCollection;
            }
            set
            {
                this.dataCollection = value;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //I need to change Background of Perticular Button
            //Based On Content of Button [Here that is "first", "second", "third", "forth"]

            //How to change the color of perticular button based on Content of Button?
        }
    }

    public class Data 
    {
        private string dataToPresent;

        public Data()
        {

        }
        public Data(string dataArg)
        {
            this.dataToPresent = dataArg;
        }
        public string DataToPresent 
        {
            get
            {
                return this.dataToPresent;
            }
            set
            {
                this.dataToPresent = value;
            }
        }
    }
XAML:


代码隐藏:

public partial class MainWindow : Window
    {
        private ObservableCollection<Data> dataCollection ;

        public MainWindow()
        {
            InitializeComponent();
            dataCollection = new ObservableCollection<Data>();

            dataCollection.Add(new Data("first"));
            dataCollection.Add(new Data("second"));
            dataCollection.Add(new Data("third"));
            dataCollection.Add(new Data("forth"));
            this.DataContext = this;
        }

        public ObservableCollection<Data> DataCollection
        {
            get
            {
                return this.dataCollection;
            }
            set
            {
                this.dataCollection = value;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //I need to change Background of Perticular Button
            //Based On Content of Button [Here that is "first", "second", "third", "forth"]

            //How to change the color of perticular button based on Content of Button?
        }
    }

    public class Data 
    {
        private string dataToPresent;

        public Data()
        {

        }
        public Data(string dataArg)
        {
            this.dataToPresent = dataArg;
        }
        public string DataToPresent 
        {
            get
            {
                return this.dataToPresent;
            }
            set
            {
                this.dataToPresent = value;
            }
        }
    }
公共部分类主窗口:窗口
{
私有可观察收集数据收集;
公共主窗口()
{
初始化组件();
dataCollection=新的ObservableCollection();
数据收集。添加(新数据(“第一”);
数据收集。添加(新数据(“第二”);
数据收集。添加(新数据(“第三”);
数据收集。添加(新数据(“第四”);
this.DataContext=this;
}
公共可观测收集数据收集
{
收到
{
返回此.dataCollection;
}
设置
{
this.dataCollection=值;
}
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
//我需要更改光圈按钮的背景
//基于按钮的内容[此处为“第一”、“第二”、“第三”、“第四”]
//如何根据按钮的内容更改垂直按钮的颜色?
}
}
公共类数据
{
私有字符串dataToPresent;
公共数据()
{
}
公共数据(字符串dataArg)
{
this.dataToPresent=dataArg;
}
公共字符串DataToPresent
{
收到
{
返回this.dataToPresent;
}
设置
{
this.dataToPresent=值;
}
}
}
屏幕截图:

public partial class MainWindow : Window
    {
        private ObservableCollection<Data> dataCollection ;

        public MainWindow()
        {
            InitializeComponent();
            dataCollection = new ObservableCollection<Data>();

            dataCollection.Add(new Data("first"));
            dataCollection.Add(new Data("second"));
            dataCollection.Add(new Data("third"));
            dataCollection.Add(new Data("forth"));
            this.DataContext = this;
        }

        public ObservableCollection<Data> DataCollection
        {
            get
            {
                return this.dataCollection;
            }
            set
            {
                this.dataCollection = value;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //I need to change Background of Perticular Button
            //Based On Content of Button [Here that is "first", "second", "third", "forth"]

            //How to change the color of perticular button based on Content of Button?
        }
    }

    public class Data 
    {
        private string dataToPresent;

        public Data()
        {

        }
        public Data(string dataArg)
        {
            this.dataToPresent = dataArg;
        }
        public string DataToPresent 
        {
            get
            {
                return this.dataToPresent;
            }
            set
            {
                this.dataToPresent = value;
            }
        }
    }
示例:

var buttonFirst = FindVisualChildren<Button>(itemControlProblem)
    .Single(b => b.Content.ToString() == "first");
buttonFirst.Background = Brushes.Yellow;

for(int i=0;i

这是可行的,但我不建议这样做,设置一个带有后台属性的viewmodel,然后绑定它会更干净。

使用此方法查找可视子对象:

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}
公共静态IEnumerable FindVisualChildren(DependencyObject depObj),其中T:DependencyObject
{
if(depObj!=null)
{
for(int i=0;i
例如:

var buttonFirst = FindVisualChildren<Button>(itemControlProblem)
    .Single(b => b.Content.ToString() == "first");
buttonFirst.Background = Brushes.Yellow;
var buttonFirst=FindVisualChildren(itemControlProblem)
.Single(b=>b.Content.ToString()=“first”);
buttonFirst.Background=画笔.黄色;