c# mvvm - 将模型数据绑定到对话框文本

问题描述 投票:0回答:1

我正在尝试将模型中的数据绑定到对话框上 TextBlock 的 Text 属性,(尽可能)尊重 mvvm 模式。但奇怪的是,没有显示任何文字。

我对用户控件也有同样的原理,效果很好。

视图模型(VM_WizardPrestage)

LogEvents.Add("Server Name : " + Prestage.ServerName);
var dialog = new DialogBox(); // my original code
//var dialog = new DialogBox { DataContext = Prestage }; // @Clemens advice, to be tested by means of what I have to put in the constructor or in the xaml

bool? result = dialog.ShowDialog();

模型(M_Prestage 在 Prestage 中实例化)

public class M_Prestage
{
    public string ServerName { get; set; }
...
}

对话框构造函数

public DialogBox()
{
    // What can i do here ?
    InitializeComponent();
}

查看

<Window x:Class="PRESTAGE.Views.DialogBox"
        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"
        xmlns:local="clr-namespace:PRESTAGE.Views"
        mc:Ignorable="d"
        Title="DialogBox" Height="150" Width="400" WindowStyle="None" WindowStartupLocation="CenterScreen" Background="Transparent" ResizeMode="NoResize" MouseDown="Window_MouseDown" AllowsTransparency="True">
    <Window.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="IsEnabled" Value="True"/>
            <Setter Property="Background" Value="#FFECE1E1"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#FF022A79"/>
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
                
            </Style.Triggers>
        </Style>
    </Window.Resources>
    
    <Grid>
        <Border  CornerRadius="12" BorderThickness="2" BorderBrush="#FF022A79">
            <Border.Background>
                <ImageBrush ImageSource="/Images/banner_prestage.png"
                            Stretch="Uniform" Opacity="1"/>
            </Border.Background>
            <Border CornerRadius="10"                    
                    BorderThickness="0"
                    Opacity="0.95">
                <Border.Background>
                    <LinearGradientBrush StartPoint="0,1" EndPoint="0,5">
                        <GradientStop Color="White" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </Border.Background>
                <StackPanel >
                    <StackPanel Margin="0,20,0,0">
                        <TextBlock Text="Would you like to change the server name?" HorizontalAlignment="Center" Foreground="#FF022A79"></TextBlock>
                    </StackPanel>
                    <StackPanel Margin="0,20,0,0">
                        <TextBlock Text="{Binding Path=Prestage.ServerName, NotifyOnTargetUpdated=True}" HorizontalAlignment="Center" Foreground="#FF022A79"></TextBlock>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Height="48" Margin="0,20,0,0">
                        <StackPanel VerticalAlignment="Center" Width="242">
                            <Button x:Name="Oui" Width="226" Height="30" Content="OUI" HorizontalAlignment="Right" Cursor="Hand" Margin="0,0,9,0" Click="Oui_Click">

                                <Button.Template>
                                    <ControlTemplate TargetType="Button">
                                        <Border Width="73" Height="30" CornerRadius="10"  Background="{TemplateBinding Background}">
                                            <ContentPresenter   VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                        </Border>
                                    </ControlTemplate>
                                </Button.Template>
                            </Button>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Width="158">
                            <Button x:Name="Non" Width="73" Height="30" Content="NON" HorizontalAlignment="Right" Cursor="Hand" VerticalAlignment="Center" Click="Non_Click">

                                <Button.Template>
                                    <ControlTemplate TargetType="Button">
                                        <Border Width="73" Height="30" CornerRadius="10"  Background="{TemplateBinding Background}">
                                            <ContentPresenter   VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                        </Border>
                                    </ControlTemplate>
                                </Button.Template>
                            </Button>
                        </StackPanel>
                    </StackPanel>
                </StackPanel>




            </Border>
        </Border>
    </Grid>
</Window>

预期:我想在对话框中显示 Prestage 模型中的数据“ServerName”。

结果:加载对话框时,不显示服务器名称。该框架是空的,因此除了问题和按钮之外您看不到任何内容。

另一方面,日志确实显示相同的 Prestage.ServerName 模型数据

提前感谢您的帮助

c# wpf mvvm binding
1个回答
0
投票

好的,所以我最终根据您的指示找到了一个很好的折衷方案。 我不知道为什么,但从一开始我就试图将模型数据直接绑定到对话框视图。 可能是因为我接手的应用程序更像是一个非常初学者的 MVVM(像我一样),并且一些数据通常直接从实例化的模型传递到视图,如下面的 Prestage.ServerName

LogEvents.Add("Server Name: " + Prestage.ServerName);

顺便说一句,我忘记了,要成为 MVVM,您首先必须传递(如果我错了,请纠正我)一个 ViewModel 属性。 这是我的功能修改

ViewModel / 添加 ServerName 属性

public string ServerName
{
    get { return Prestage.ServerName; }
    set
    {
        Prestage.ServerName = value;
        OnPropertyChanged(nameof(ServerName));
    }
}

ViewModel / DialogBox 实例化

var dialog = new DialogBox { DataContext = this };
bool? result = dialog.ShowDialog();

视图/对话框

<StackPanel Margin="0,20,0,0">
    <TextBlock Text="{Binding Path=ServerName, Mode=TwoWay, NotifyOnTargetUpdated=True}" HorizontalAlignment="Center" Foreground="#FF022A79"></TextBlock>
</StackPanel>

就是这样,除非与缺少的 mode=TwoWay 有关,否则无论如何它都可以正常工作! 再次感谢您的评论

© www.soinside.com 2019 - 2024. All rights reserved.