Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将图像集合指定给代码隐藏属性_C#_Wpf_Xaml_Collections - Fatal编程技术网

C# 将图像集合指定给代码隐藏属性

C# 将图像集合指定给代码隐藏属性,c#,wpf,xaml,collections,C#,Wpf,Xaml,Collections,好的,我已经花了好几天的时间来弄清楚XAML实际上是如何处理集合的,每次我尝试DependencyProperty方法时,属性都是空的。当我使用ObservaleCollection并在XAML中为其分配一个数组类型时,我会收到一条消息,说明不能将类型ArrayExtension分配给collection或dictionary 所以我现在坐在一个点上,我不知道我不知道什么,但我知道我的目标 我想在代码隐藏中为我的集合分配多个类型的元素,并且我必须能够识别(最好是通过整数)集合中引用的“图像” 最

好的,我已经花了好几天的时间来弄清楚XAML实际上是如何处理集合的,每次我尝试DependencyProperty方法时,属性都是空的。当我使用ObservaleCollection并在XAML中为其分配一个数组类型时,我会收到一条消息,说明不能将类型ArrayExtension分配给collection或dictionary

所以我现在坐在一个点上,我不知道我不知道什么,但我知道我的目标

我想在代码隐藏中为我的集合分配多个
类型的元素,并且我必须能够识别(最好是通过整数)集合中引用的“图像”

最好是XAML格式

<cc:MyControl.States>
    <Image Source="{StaticResource PointBulletIconImage}" />
    <Image Source="{StaticResource PointNumberIconImage}" />
</cc:MyControl.States>

控件应该声明一个属性来保存
ImageSource
对象的集合,而不是图像控件的集合,如

public partial class MyControl : UserControl
{
    public List<ImageSource> States { get; } = new List<ImageSource>();

    ...
}
您可以在code behind中设置它的
Source
属性,如下所示:

<cc:MyControl>
    <cc:MyControl.States>
        <StaticResource ResourceKey="PointBulletIconImage"/>
        <StaticResource ResourceKey="PointNumberIconImage"/>
    </cc:MyControl.States>
</cc:MyControl>
image.Source = States[0];

控件应该声明一个属性来保存
ImageSource
对象的集合,而不是图像控件的集合,如

public partial class MyControl : UserControl
{
    public List<ImageSource> States { get; } = new List<ImageSource>();

    ...
}
您可以在code behind中设置它的
Source
属性,如下所示:

<cc:MyControl>
    <cc:MyControl.States>
        <StaticResource ResourceKey="PointBulletIconImage"/>
        <StaticResource ResourceKey="PointNumberIconImage"/>
    </cc:MyControl.States>
</cc:MyControl>
image.Source = States[0];

鉴于您的姓名,我怀疑您希望的行为是根据状态属性显示不同的图标,在这种情况下,您确实希望尽可能不使用代码

我建议使用一个值转换器,在其中输入状态并返回一个图像源或URI,然后将图像源绑定到status属性

下面是一个完整的示例,说明这是什么样子 注意:示例使用c#6语法

WPF:
代码:

公共枚举状态
{
国家1,
国家2,
国家3,
国家4,
国家5,
}
公共类StateImageConverter:IValueConverter
{
公共ImageSource State1Image{get;set;}
公共ImageSource状态2图像{get;set;}
公共ImageSource State3Image{get;set;}
公共ImageSource State4Image{get;set;}
公共ImageSource State5Image{get;set;}
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
var状态=状态值?;
if(state.HasValue)
{
开关(状态值)
{
案例国家1:
返回状态1图像;
案例国家2:
返回状态2图像;
案例国家3:
返回状态3图像;
案例国家4:
返回状态4image;
案例国家5:
返回状态5图像;
违约:
抛出新的InvalidCastException();
}
}
其他的
抛出新的NotImplementedException();
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
//单程
抛出新的NotImplementedException();
}
}
公共类VeiwModel:System.ComponentModel.INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私人国家;
公共国家
{
获取{返回状态;}
设置
{
状态=值;
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(“状态”);
}
}
public IEnumerable AllStates=>Enum.GetValues(typeof(States))。of type();
}
或者,如果不想使用命名属性,则使用列表的变体如下所示:

public class StateImageConverter : IValueConverter
{
    public ObservableCollection<ImageSource> Images { get; set; } = new ObservableCollection<ImageSource>();
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var state = value as States?;
        if(state.HasValue)
        {
            return Images[(int)state.Value];
        }
        else
            throw new NotImplementedException();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //one way only
        throw new NotImplementedException();
    }
}
公共类StateImageConverter:IValueConverter
{
公共ObservableCollection映像{get;set;}=new ObservableCollection();
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
var状态=状态值?;
if(state.HasValue)
{
返回图像[(int)state.Value];
}
其他的
抛出新的NotImplementedException();
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
//单程
抛出新的NotImplementedException();
}
}
定义为

<local:StateImageConverter 
            x:Key="StateImageConverter">
            <local:StateImageConverter.Images>
                <BitmapImage UriSource="someimg1.png" />
                <BitmapImage UriSource="someimg2.png" />
                <BitmapImage UriSource="someimg3.png" />
                <BitmapImage UriSource="someimg4.png" />
                <BitmapImage UriSource="someimg5.png" />
            </local:StateImageConverter.Images>
        </local:StateImageConverter>

鉴于您的姓名,我怀疑您希望的行为是根据状态属性显示不同的图标,在这种情况下,您确实希望尽可能不使用代码

我建议使用一个值转换器,在其中输入状态并返回一个图像源或URI,然后将图像源绑定到status属性

下面是一个完整的示例,说明这是什么样子 注意:示例使用c#6语法

WPF:
代码:

公共枚举状态
{
国家1,
国家2,
国家3,
国家4,
国家5,
}
公共类StateImageConverter:IValueConverter
{
公共ImageSource State1Image{get;set;}
公共ImageSource状态2图像{get;set;}
公共ImageSource State3Image{get;set;}
公共ImageSource State4Image{get;set;}
公共ImageSource State5Image{get;set;}
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
var状态=状态值?;
if(state.HasValue)
{
开关(状态值)
{
案例国家1:
返回状态1图像;
案例国家2:
返回状态2图像;
案例国家3:
返回状态3图像;
案例街
<local:StateImageConverter 
            x:Key="StateImageConverter">
            <local:StateImageConverter.Images>
                <BitmapImage UriSource="someimg1.png" />
                <BitmapImage UriSource="someimg2.png" />
                <BitmapImage UriSource="someimg3.png" />
                <BitmapImage UriSource="someimg4.png" />
                <BitmapImage UriSource="someimg5.png" />
            </local:StateImageConverter.Images>
        </local:StateImageConverter>
public ImageSource ImageSource {
        get {
            return GetValue(ImageSourceProperty) as ImageSource;
        }
        set {
            SetValue(ImageSourceProperty, value);
        }
    }
    public ObservableCollection<ImageSource> States { get; } = new ObservableCollection<ImageSource>();

    public int CurrentState { get; set; }
    public static readonly DependencyProperty ImageSourceProperty  = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(PointButton));
    public PointButton( ) {
        CurrentState = 0;
        InitializeComponent( );
        Loaded += PointButton_Loaded;
    }

    private void PointButton_Loaded(object sender, RoutedEventArgs e) {
        PreviewMouseUp += PointButton_MouseUp;
        UpdateLayout( );
        if(States == null)
            Console.WriteLine("States is null");
        else
            ImageSource = States[CurrentState];
    }

    private void PointButton_MouseUp(object sender, MouseButtonEventArgs e) {
        if(CurrentState == States.Count - 1)
            CurrentState = 0;
        else
            CurrentState += 1;
        ImageSource = States[CurrentState];
    }
}
<Button x:Class="Notes.Views.Controls.PointButton"
    x:Name="RootElement"
         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"
         xmlns:local="clr-namespace:Notes.Views.Controls"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300"
         >
<Button.Template>
    <ControlTemplate>
        <Image Source="{Binding ImageSource, ElementName=RootElement}" />
    </ControlTemplate>
</Button.Template>