C#Prism:从控制器(MVVM)设置ViewModel属性

时间:2016-06-24 20:50:05

标签: c# wpf mvvm prism

ViewModel:

...
auto id = QFontDatabase::addApplicationFont(":/fonts/myfont.ttf");
if (if == -1)
  return;
auto families = QFontDatabase::applicationFontFamilies(id);
if (families.isEmpty())
  return;
auto face = families.first().toHtmlEscaped();
auto html = QStringLiteral("<font face=\"%1\">Hello</font>").arg(face);
...

XAML:

public class ConnectionStatusViewModel : BindableBase
{

    private string _txtConn;

    public string TextConn
    {
        get { return _txtConn; }
        set { SetProperty(ref _txtConn, value); }
    }
}

观点:

<UserControl x:Class="k7Bot.Login.Views.ConnectionStatus"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:prism="http://www.codeplex.com/prism"
            prism:ViewModelLocator.AutoWireViewModel="True" Width="300">

    <Grid x:Name="LayoutRoot">    
        <Label Grid.Row="1" Margin="10,0,10,0">connected:</Label>
        <TextBlock Text="{Binding TextConn}"  Grid.Row="1" Grid.Column="1" Margin="10,0,10,0" Height="22" />
    </Grid>

</UserControl>

在另一个模块中,我有一个事件监听器,最终运行此代码:

public partial class ConnectionStatus : UserControl
{
    public ConnectionStatus()
    {
        InitializeComponent();
    }
}

代码运行但ConnectionStatusViewModel viewModel = _connectionView.DataContext as ConnectionStatusViewModel; if (viewModel != null) { viewModel.TextConn = "Testing 123"; } 已更新,但未显示在UI

3 个答案:

答案 0 :(得分:0)

你确定TextConn没有更新吗?因为它可以更新,但显示无法更改。您应该实现INotifyPropertyChanged接口,并在对TextConn进行任何更改后调用已实现的OnPropertyChanged(&#34; TextConn&#34;);或者你命名的功能。这将告诉UI该值已更改并且需要更新。

答案 1 :(得分:0)

初始化UC时,UserControl的DataContext获取其值。然后,您将获得DataContext的副本,将其强制转换为视图模型对象,然后更改属性。我不相信UC在这种情况下更新了原始的DataContext。

可能您需要使用消息中介来传达不同模块之间的更改。

答案 2 :(得分:0)

经过一些故障排除后,这段代码可以运行,问题是我运行的是这段代码:

import pandas as pd
import os
dirListing = os.listdir("D:/Python/Test/Diku/piwik/filteredcsv/")

df=[]
siteIds = [34]
for id in siteIds: 
    for item in dirListing:
        if str(id) in item:
            print item        
            df.append(pd.read_csv(item,sep = ",",dtype='unicode')) 

    df3 = pd.concat(df,axis=0, ignore_index=True)
    df3.to_csv('merged_' + str(id) + '_raw'+'.csv', sep =',')

在视图实际激活之前。愚蠢,但也许它会帮助某人下线。