如何将数据从View绑定到ViewModel?

时间:2016-03-25 11:54:52

标签: wpf xaml mvvm prism

我对mvvm知之甚少,但这就是我编写代码的方式:

<Image x:Name ="new_tooltip" Grid.Row="84" Grid.Column="57" Grid.ColumnSpan="78"  Grid.RowSpan="15"  Source="/MS_Show_Assets/MainMenuAssets/TT-Startscreen-MainMenu-New-DE.png" Visibility = "{Binding IsMouseOver, ElementName=New, Converter={StaticResource BooleanToVisibilityConverter}}">
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Setter Property="Visibility" Value="{Binding Path=IsNewTooltipVisible, Mode=OneWayToSource}" />
                </Style>
            </Image.Style>
        </Image>

和ViewModel:

public class ViewMainMenuViewModel : BindableBase
    {
        public string IsNewTooltipVisible { get; set; }

        public ViewMainMenuViewModel()
        {

        }
    }

所以基本上,如果鼠标悬停在某个按钮上,我希望视图中的某些图像变得可见。然后,一旦这个图像可见,我想发送&#34;可见&#34;到ViewModel类中的属性。我在课堂上还缺少什么?

1 个答案:

答案 0 :(得分:1)

你不需要VM中的属性来执行此操作。您可以使用“在视图上触发”按钮鼠标悬停时显示/隐藏图像,如下所示。这里ElementName是要捕获其鼠标悬停的按钮的名称。

<Image x:Name ="new_tooltip" Grid.Row="84" Grid.Column="57" Grid.ColumnSpan="78"  Grid.RowSpan="15"  Source="/MS_Show_Assets/MainMenuAssets/TT-Startscreen-MainMenu-New-DE.png" Visibility = "{Binding IsMouseOver, ElementName=New, Converter={StaticResource BooleanToVisibilityConverter}}">
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Setter Property="Visibility" Value="Collapsed"></Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsMouseOver, ElementName=myButton}" Value="true">
                            <Setter Property="Visibility" Value="Visible"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>