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

发布于 2024-11-25 00:22:02 字数 3761 浏览 0 评论 0原文

我有以下代码。

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

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

看看下面的代码和屏幕截图:

XAML:

<Window x:Class="ItemControlProblem.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">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50*" />
            <RowDefinition Height="261*" />
        </Grid.RowDefinitions>
        <ItemsControl Name="itemControlProblem" ItemsSource="{Binding Path=DataCollection}" Grid.RowSpan="2">
            <ItemsControl.Template>
                <ControlTemplate TargetType="ItemsControl">
                    <Border>
                        <StackPanel>
                            <ItemsPresenter></ItemsPresenter>
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"></StackPanel>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding Path=DataToPresent}"></Button>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <Button Content="ChangeBackGround" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="215,104,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

后面的代码:

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;
            }
        }
    }

屏幕截图:

在此处输入图像描述

I have the following code.

In my Example I have Displaying Buttons using ItemControl.
Now i need to access particular button based on its content
and change its property.

say,, i nee to change Background property of first button when ChangeBackGround button being Clicked. [See Screen shot at very below at my Question]

Have a look at below code and Screen Shot :

XAML:

<Window x:Class="ItemControlProblem.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">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50*" />
            <RowDefinition Height="261*" />
        </Grid.RowDefinitions>
        <ItemsControl Name="itemControlProblem" ItemsSource="{Binding Path=DataCollection}" Grid.RowSpan="2">
            <ItemsControl.Template>
                <ControlTemplate TargetType="ItemsControl">
                    <Border>
                        <StackPanel>
                            <ItemsPresenter></ItemsPresenter>
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"></StackPanel>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding Path=DataToPresent}"></Button>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <Button Content="ChangeBackGround" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="215,104,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

CODE BEHIND:

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;
            }
        }
    }

Screen Shot:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

波浪屿的海角声 2024-12-02 00:22:03

使用此方法查找视觉儿童:

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;
            }
        }
    }
}

示例:

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

Use this method to find visual children:

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;
            }
        }
    }
}

Example:

var buttonFirst = FindVisualChildren<Button>(itemControlProblem)
    .Single(b => b.Content.ToString() == "first");
buttonFirst.Background = Brushes.Yellow;
挽清梦 2024-12-02 00:22:03

示例:

<ItemsControl Name="ictest" ItemsSource="{Binding DpData}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Name="buton" Content="{Binding Name}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
for (int i = 0; i < ictest.Items.Count; i++)
{
    var container = ictest.ItemContainerGenerator.ContainerFromIndex(i) as ContentPresenter;
    var button = container.ContentTemplate.FindName("buton", container) as Button;
    if (button.Content == "Skeet")
    {
        button.Background = Brushes.Red;
    }
}

这可行,但我不建议做这样的事情,设置一个带有背景属性的视图模型,然后绑定它会更干净。

Example:

<ItemsControl Name="ictest" ItemsSource="{Binding DpData}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Name="buton" Content="{Binding Name}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
for (int i = 0; i < ictest.Items.Count; i++)
{
    var container = ictest.ItemContainerGenerator.ContainerFromIndex(i) as ContentPresenter;
    var button = container.ContentTemplate.FindName("buton", container) as Button;
    if (button.Content == "Skeet")
    {
        button.Background = Brushes.Red;
    }
}

This works, but i would not suggest doing something like this, setting up a viewmodel with a background-property which then is bound would be much cleaner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文