如何动态绑定UniformGrid中控件的事件和属性?

时间:2015-06-01 09:16:50

标签: c# wpf xaml caliburn.micro

我是模仿Wpf控件的新手。我使用VS2013,WPF 4.5和 Caliburn Micro 2.0.2 。在我的部分任务中,我需要使用包含不同图像及其副标题的切换按钮来填充网格。我用UniformGrid解决了它。请参阅下面的代码。他们工作但仍然没有事件和属性绑定,因为我不知道如何将切换按钮的事件和属性绑定到视图模型,因为它们是自动生成的,并且是动态生成的以及切换按钮的数量不确定(取决于图像文件夹中的图像数量)。

例如: 手动我可以将Click事件,IsChecked属性和切换按钮1的其他一些属性绑定如下:

<ToggleButton x:Name="ToggleVehicle01" IsChecked={Binding SelectedVehicle01} Background="{Binding BackColorSelectedVehicle01}" ToolTip="{Binding VehicleName01}">

现在我不能再这样做了,因为切换按钮是自动生成的,而且它们的数量是不确定的。请帮忙。 随时更改我的代码或者给我一些有用的示例代码。提前谢谢。

视图(MainView.xaml):

<UserControl x:Class="CMWpf02.Views.MainView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         d:DesignHeight="300"
         d:DesignWidth="300"
         mc:Ignorable="d">
    <Grid Width="1024"
          Height="768"
          HorizontalAlignment="Left"
          VerticalAlignment="Top"
          ShowGridLines="True">
        <ItemsControl Name="ImageList"
                      Background="#FFFFFFFF"
                      BorderBrush="#FFA90606"
                      ItemsSource="{Binding Path=VehicleImages}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Margin="0,0,0,0" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ToggleButton Width="180"
                                  Margin="10,10,10,10"
                                  FontSize="10"
                                  Style="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}">
                        <!--  x:Name="ToggleVehicle01"  -->
                        <!--  Background="{Binding BackColorSelectedVehicle01}"  -->
                        <!--  IsChecked="{Binding SelectedVehicle01}"  -->
                        <!--  ToolTip="{Binding Vehicle01Name}">  -->
                        <StackPanel Margin="0,5,0,5"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center">
                            <Image Width="140"
                                   RenderOptions.BitmapScalingMode="Fant"
                                   Source="{Binding Path=Image}" />
                            <TextBlock HorizontalAlignment="Center"
                                       VerticalAlignment="Center"
                                       FontWeight="Bold"
                                       Text="{Binding Path=Name}" />
                        </StackPanel>
                    </ToggleButton>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</UserControl>

ViewModel(MainViewModel.cs):

using Caliburn.Micro;
using System;
using System.Collections.ObjectModel;
using System.IO;

namespace CMWpf02.ViewModels
{
    public class MainViewModel : Screen, IHaveDisplayName
    {
        private String _path2Images = @"D:\tmp\Images";
        public string DisplayName { get; set; }
        public ObservableCollection<VehicleImage> VehicleImages { get; set; }

        public MainViewModel()
        {
            DisplayName = "Main Window";
            var vehicles = new ObservableCollection<String>();
            vehicles = GetAllFilesFromFolder(_path2Images);
            VehicleImages = new ObservableCollection<VehicleImage>();
            foreach (var i in vehicles)
                VehicleImages.Add(new VehicleImage(i));
        }

        public ObservableCollection<String> GetAllFilesFromFolder(String fullPathFolder)
        {
            string[] fileArray = Directory.GetFiles(fullPathFolder);
            return new ObservableCollection<String>(fileArray);
        }
    }

    public class VehicleImage
    {
        public String Image { get; private set; }
        public String Name { get; private set; }

        public VehicleImage(String image)
        {
            Image = image;
            Name = Path.GetFileName(image);
        }
    }
    //public void ToggleVehicle01()
    //{
    //    var selectText = (SelectedVehicle01) ? " selected" : " unselected";
    //    MessageBox.Show(Vehicle01Name + selectText);
    //    BackColorSelectedVehicle01 = (SelectedVehicle01) ? _backColorSelectedVehicle : _defaultBackColorVehicle;
    //}

    //public Boolean SelectedVehicle02
    //{
    //    get { return _selectedVehicle02; }
    //    set
    //    {
    //        _selectedVehicle02 = value;
    //        NotifyOfPropertyChange(() => SelectedVehicle02);
    //    }
    //}

    //public Brush BackColorSelectedVehicle02
    //{
    //    get { return _backColorSelectedVehicle02; }
    //    set
    //    {
    //        _backColorSelectedVehicle02 = value;
    //        NotifyOfPropertyChange(() => BackColorSelectedVehicle02);
    //    }
    //public String Vehicle01Name { get; private set; }
}

编辑:现在我可以将生成的ToggleButton的属性与视图模型绑定。我将VehicleImage类设置为视图模型(请参阅下面的修改代码)。但是我仍然有问题来绑定生成的ToggleButton 的Click事件来查看模型。

已修改类以查看模型

public class VehicleImage : PropertyChangedBase
{
    public String Image { get; private set; }
    public String Name { get; private set; }

    private Boolean _selectedVehicle;
    public Boolean SelectedVehicle
    {
        get { return _selectedVehicle; }
        set
        {
            _selectedVehicle = value;
            BackColorSelectedVehicle = _selectedVehicle ? new SolidColorBrush(Color.FromArgb(255, 242, 103, 33)) : new SolidColorBrush(Colors.White);
        }
    }

    private Brush _backColorSelectedVehicle;
    public Brush BackColorSelectedVehicle
    {
        get { return _backColorSelectedVehicle; }
        set
        {
            _backColorSelectedVehicle = value;
            NotifyOfPropertyChange(() => BackColorSelectedVehicle);
        }
    }

    // ToggleButton's Click-Event Handler, but it doesn't get event trigger from View.
    // Therefore I set the BackColorSelectedVehicle fin setter of SelectedVehicle property.
    public void ToggleSelection()
    {
        //BackColorSelectedVehicle = SelectedVehicle ? new SolidColorBrush(Color.FromArgb(255, 242, 103, 33)) : new SolidColorBrush(Colors.White);
    }

    public VehicleImage(String image)
    {
        Image = image;
        Name = Path.GetFileName(image);
    }
}

已修改视图

<ToggleButton Width="180"
              Margin="10,10,10,10"
              Background="{Binding Path=BackColorSelectedVehicle}"
              FontSize="10"
              IsChecked="{Binding Path=SelectedVehicle}"
              Style="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}"
              ToolTip="{Binding Path=Name}">
              <!--  x:Name="ToggleSelection"  -->
    <StackPanel Margin="0,5,0,5"
                HorizontalAlignment="Center"
                VerticalAlignment="Center">
        <Image Width="140"
               RenderOptions.BitmapScalingMode="Fant"
               Source="{Binding Path=Image}" />
               <TextBlock HorizontalAlignment="Center"
                          VerticalAlignment="Center"
                          Text="{Binding Path=Name}" />
    </StackPanel>
</ToggleButton>

0 个答案:

没有答案