使用mvvm单击wpf中的按钮时如何发送所选图像值? [英] How to send the selected the image value when click the button in wpf using mvvm?

查看:45
本文介绍了使用mvvm单击wpf中的按钮时如何发送所选图像值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SampleView.xaml:



< usercontrol x:class =GameDealModule.View.SampleViewxmlns:x =#unknown>

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

mc:Ignorable =d

d:DesignHeight =600d:DesignWidth =1241> ;



< grid removed =Grayhorizo​​ntalalignment =Centerverticalalignment =Center>

< Canvas Horizo​​ntalAlignment = LeftHeight =600Margin =29,10,0,0VerticalAlignment =TopWidth =1241>



< rectangle height =209width =150canvas.left =41stroke =Blackcanvas.top =21>



< Image Height =209Width =150Source =../ Images / nature.pngCanvas.Left =41Canvas。顶= 21 >< /图像>







< rectangle height =209width =150canvas.left = 206stroke =Blackcanvas.top =21>



< Image Height =209Width =150Source =.. /Images/flower.pngCanvas.Left =206Canvas.Top =21>< / Image>





< Button Content =SubmitCanvas.Left =372Canvas.Top =533Width =103Height =37Command ={Binding SubmitCommand}/>

< Button Content =CloseCanvas.Left =489Canvas.Top =533Width =103Height =37Command ={Binding CloseCommand}/>



< / Canvas>







SampleView .xaml.cs:

公共分部类SampleView:UserControl

{

public SampleView()

{

InitializeComponent();

this.DataContext = new SampleViewModel( );

}

}



SampleViewModel.cs:

公共课SampleViewModel:INotifyPropertyChanged

{

public SampleViewModel()

{



this .SubmitCommand = new DelegateCommand< object>(this.OnSubmit,this.CanSubmit);



}

private bool CanSubmit(object arg)

{

返回true;

}





private void OnSubmit(object arg)

{

string selectedimage =;



}

public ICommand SubmitCommand {get;私人集; }





#region INotifyPropertyChanged会员



公共事件PropertyChangedEventHandler PropertyChanged;



private void NotifyPropertyChanged(string propertyName)

{

if(this.PropertyChanged!= null)

{

this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName));

}

}



#endregion

}

SampleView.xaml:

<usercontrol x:class="GameDealModule.View.SampleView" xmlns:x="#unknown">
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"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="1241" >

<grid removed="Gray" horizontalalignment="Center" verticalalignment="Center">
<Canvas HorizontalAlignment="Left" Height="600" Margin="29,10,0,0" VerticalAlignment="Top" Width="1241">

<rectangle height="209" width="150" canvas.left="41" stroke="Black" canvas.top="21">

<Image Height="209" Width="150" Source="../Images/nature.png" Canvas.Left="41" Canvas.Top="21"></Image>



<rectangle height="209" width="150" canvas.left="206" stroke="Black" canvas.top="21">

<Image Height="209" Width="150" Source="../Images/flower.png" Canvas.Left="206" Canvas.Top="21"></Image>


<Button Content="Submit" Canvas.Left="372" Canvas.Top="533" Width="103" Height="37" Command="{Binding SubmitCommand}"/>
<Button Content="Close" Canvas.Left="489" Canvas.Top="533" Width="103" Height="37" Command="{Binding CloseCommand}"/>

</Canvas>



SampleView.xaml.cs:
public partial class SampleView: UserControl
{
public SampleView()
{
InitializeComponent();
this.DataContext = new SampleViewModel();
}
}

SampleViewModel.cs:
public class SampleViewModel: INotifyPropertyChanged
{
public SampleViewModel()
{

this.SubmitCommand = new DelegateCommand<object>(this.OnSubmit, this.CanSubmit);

}
private bool CanSubmit(object arg)
{
return true;
}


private void OnSubmit(object arg)
{
string selectedimage="";

}
public ICommand SubmitCommand { get; private set; }


#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

#endregion
}

推荐答案





我不认为Image有选择的属性,你可以使用单选按钮,并用图像编辑它的模板。因此,您一次只能选择一个图像(如果您的场景允许多选意味着您可能需要使用任何其他控件)。



Xaml代码将如下所示,

Hi,

I don't think Image is having selected property, you can use radio button instead and edit its template with the images. So you can select only one image at a time (if your scenario allows multiselect means you may need to use any other controls).

Xaml code will be like this,
<RadioButton x:Name="nature" Height="100" Width="100">
            <RadioButton.Template>
                <ControlTemplate>
                    <Image Height="100" Width="100" Source="../Images/images.jpg" HorizontalAlignment="Center"/>
                </ControlTemplate>
            </RadioButton.Template>
        </RadioButton>





在RadioButton的行为中,在Checked事件中,您可以使用radiobutton名称设置ViewModel的SelectedImage属性。要了解行为,请访问此链接。



希望它可以帮到你。



问候,

Karuppasamy P



And in the behavior of RadioButton, at the Checked event, you can set the SelectedImage property of ViewModel with the radiobutton name. To know about behavior go to this link.

Hope it helps you.

Regards,
Karuppasamy P


这篇关于使用mvvm单击wpf中的按钮时如何发送所选图像值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆