简体   繁体   English

如何更改绑定 WPF class 变量的值?

[英]How do I change the value of a bound WPF class variable?

I am new to WPF/XAML.我是 WPF/XAML 的新手。

I am making a scrolling marquee using a demo I found online.我正在使用我在网上找到的演示制作滚动字幕。 It is WinForms using a HostedElement WPF custom User Control.它是使用 HostedElement WPF 自定义用户控件的 WinForms。

I was able to setup a class called MyModelView so that I can bind the text value of a textblock to a variable I control (MyTextProperty).我能够设置一个名为 MyModelView 的 class,这样我就可以将文本块的文本值绑定到我控制的变量 (MyTextProperty)。

But I can't figure out how to change the variable.但我不知道如何更改变量。 My goal is to have a textbox you can enter text into and it will bind to the textblock value and change it while the program is running.我的目标是有一个文本框,您可以在其中输入文本,它将绑定到文本块值并在程序运行时更改它。

I am not able to do this, which I am trying to do (Change bound variable to TextBox1 value - it is not a valid reference to the variable... what is?):我无法做到这一点,我正在尝试这样做(将绑定变量更改为 TextBox1 值 - 它不是对该变量的有效引用......是什么?):

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    MyViewModel.MyTextProperty = TextBox1.text
End Sub

How do I reference that class variable to change the text?如何引用 class 变量来更改文本? I am missing something.我错过了一些东西。

MyViewModel.vb我的视图模型.vb

Imports System.ComponentModel

Public Class MyViewModel
    Implements INotifyPropertyChanged

    Public Sub New()
        Me.myTextValue = "WINNING!!!"
    End Sub

    Private myTextValue As String = String.Empty
    Public Property MyTextProperty() As String
        Get
            Return Me.myTextValue
        End Get

        Set(ByVal value As String)
            Me.myTextValue = value
            NotifyPropertyChanged("MyTextProperty")
        End Set
    End Property

    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

End Class

Scrolling Marque.xaml滚动 Marque.xaml

<UserControl x:Class="ScrollingMarquee"
    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:interopdemo="clr-namespace:InteropDemo"
    mc:Ignorable="d" 
    d:DesignHeight="100" d:DesignWidth="300">
    <UserControl.Resources>
        <Storyboard x:Key="MarqueeScroll">
            <DoubleAnimation RepeatBehavior="Forever" 
                Storyboard.TargetProperty="(Border.RenderTransform).(TranslateTransform.X)" 
                Storyboard.TargetName="spMarquee" 
                From="1500"  To="-500" 
                Duration="0:0:0:7" />
        </Storyboard>
    </UserControl.Resources>
    <Grid>
        <Grid.DataContext>
            <interopdemo:MyViewModel/>
        </Grid.DataContext>
        <StackPanel x:Name="spMarquee" Orientation="Horizontal"  Width="Auto">
            <TextBlock Text="{Binding MyTextProperty}" FontSize="28" VerticalAlignment="Center" Margin="30,0,60,0"/>
            <TextBlock Text="Hello Scrolling Text!" Foreground="Firebrick" FontSize="28" VerticalAlignment="Center" Margin="40,0,60,0"/>
            <TextBlock Text="Ticker 2000!" Foreground="Red" FontSize="28" FontStyle="Italic" FontWeight="Bold" VerticalAlignment="Center" Margin="50,0,80,0"/>
            <StackPanel.RenderTransform>
                <TranslateTransform/>
            </StackPanel.RenderTransform>
        </StackPanel>
    </Grid>
</UserControl>

Form1.vb Form1.vb

Imports System.Windows.Media.Animation

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.WindowState = FormWindowState.Maximized

        ElementHost1.Visible = True
        Dim storyRunMarquee As Storyboard = ScrollingMarquee1.FindResource("MarqueeScroll")
        storyRunMarquee.Begin()

    End Sub

End Class

I've tried referencing the variable but can't make it work.我试过引用变量但无法使其工作。 The scrolling animation displays as "Winning. Hello Scrolling Text! Ticker 2000!"滚动 animation 显示为“获胜。你好滚动文本!Ticker 2000!” But I'm trying to change the bound "Winning!"但是我正在尝试改变绑定的“获胜!” text.文本。

Remove消除

<Grid.DataContext>
   <interopdemo:MyViewModel/>
</Grid.DataContext>

Declare a member variable in the form to hold reference to the view model object:在窗体中声明一个成员变量来保存对视图 model object 的引用:

Private scrollerViewModel As MyViewModel

Instantiate an object of class MyViewModel in Form1_Load and set it as a DataContext for user control.Form1_Load中实例化一个class MyViewModel的object,设置为用户控件的DataContext

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
   Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
   Me.WindowState = FormWindowState.Maximized

   ElementHost1.Visible = True

   scrollerViewModel = New MyViewModel
   ScrollingMarquee1.DataContext = scrollerViewModel

   Dim storyRunMarquee As Storyboard = ScrollingMarquee1.FindResource("MarqueeScroll")
   storyRunMarquee.Begin()

End Sub

Modify property:修改属性:

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    scrollerViewModel.MyTextProperty = TextBox1.text
End Sub

I think you just need to cast the datacontext of ScrollingMarquee1 as MyViewModel我认为您只需要将 ScrollingMarquee1 的数据上下文转换为 MyViewModel

 CType(ScrollingMarquee1.DataContext, MyViewModel).MyTextProperty = TextBox1.text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM