1

i have a text block that i'm trying to data bind to a property in my viewModel, i can't get it to update, here's code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        FurnaceDataViewModel viewModel = new FurnaceDataViewModel();
        InitializeComponent();
    }
}

.

class FurnaceDataViewModel: ObservableObject
{
    public FurnaceDataModel dataModel = new FurnaceDataModel();
    public SerialPort serialPort = new SerialPort();

    public FurnaceDataViewModel()
    {            
        serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
        try
        {
            if (serialPort.IsOpen != true)
                serialPort.Close();
            serialPort.Open();
            serialPort.DataReceived += new SerialDataReceivedEventHandler(serialReceivedData); 
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    public void serialReceivedData(object sender, SerialDataReceivedEventArgs e)
    {
        dataModel.IncomingMessage = serialPort.ReadExisting();            
    }
}

.

class FurnaceDataModel: ObservableObject
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string incomingMessage;

    public string IncomingMessage
    {
        get { return incomingMessage; }
        set
        {
            incomingMessage = value;
            OnPropertyChanged("IncomingMessage");
        }
    }
}

.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:FurnaceData" x:Class="FurnaceData.MainWindow"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TextBlock x:Name="tbkMain" HorizontalAlignment="Left" Height="292" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding dataModel.IncomingMessage}" VerticalAlignment="Top" Width="489"/>
</Grid>

and how do i text wrap the text block when the data gets binding? like += kinda thing?

darthwillard
  • 809
  • 2
  • 14
  • 28
  • You can't bind to fields so change dataModel to be a property instead of just a field (http://stackoverflow.com/questions/5387206/has-it-ever-been-possible-to-bind-to-a-field-in-wpf). Also, you need to set the DataContext for your view which can be accomplished many ways (most of them better than this but keeping it simple for now). So, after the InitializeComponent call in the MainWindow constructor, add "this.DataContext = viewModel;". – Lee O. Feb 01 '14 at 21:39

1 Answers1

1

I think you need to set your datacontext or set up a objectDataProvider.

In your constructor, try adding the line

DataContext = viewModel;

And change your field to a property

public class FurnaceDataViewModel
{
    public FurnaceDataModel dataModel { get; set; }

And you should see your text wrapping, but it's hard to see when you have your textblock code set to an absolute size... try this to see it wrap

 <TextBlock x:Name="tbkMain" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Margin="10,10,0,0" TextWrapping="Wrap" 
               Text="{Binding Path=dataModel.IncomingMessage}" />
Jay
  • 9,561
  • 7
  • 51
  • 72
  • i think that means the same as dataModel.IncomingMessage, doesn't it? – darthwillard Feb 01 '14 at 21:11
  • @Jay, that binding is invalid. If you don't specify an attribute it will assume the default attribute `Path`, so now you have it twice. – Silvermind Feb 01 '14 at 21:20
  • Yeah, my bad - had a network meltdown while editing, so couldn't change what I posted... only just back online – Jay Feb 01 '14 at 21:21
  • There you go, just tried it and I can now see the text appear! You cant bind to a field (and they shouldn't be public, that is what properties are for...) – Jay Feb 01 '14 at 21:32
  • Awesome! that worked great to data bind it. how do i wrap the text so it displays all messages in the text block, like incomingMessage+= incomingMessage – darthwillard Feb 01 '14 at 21:39
  • lee, it's already set to that. it looks like i need to use string builder. – darthwillard Feb 01 '14 at 21:43
  • @darthwillard I missed that you wanted multiple messages to appear in the same field. That is going to be a different issue than a simple Textwrapping="Wrap" can solve. – Lee O. Feb 01 '14 at 21:43