【问题标题】:binding property from another cs file with converter WPF使用转换器 WPF 绑定来自另一个 cs 文件的属性
【发布时间】:2016-10-05 03:01:42
【问题描述】:

MainWindow.xaml.cs 我定义了:

private int _currentStep = 1;
public int CurrentStep
{
    get
    {
        return _currentStep;
    }

    set
    {
        _currentStep = value;
        NotifyPropertyChanged("CurrentStep");
    }
}

private void NotifyPropertyChanged(string info)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}
public event PropertyChangedEventHandler PropertyChanged;

我也在同一个文件中更新 CurrentStep。

MessageWithProgressBar.xaml 我定义了:

<Window x:Class="Db.MessageWithProgressBar"
    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:Db"
    mc:Ignorable="d"
    Title="DB Processing" Height="150" Width="300" Background="{DynamicResource {x:Static SystemColors.GradientInactiveCaptionBrushKey}}">
<Window.Resources>
    <local:currentStepToProgressValue x:Key="stepToProgress"/>
</Window.Resources>
<Grid DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}">
    <Grid.RowDefinitions>
        <RowDefinition Height="60"/>
        <RowDefinition Height="60"/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Label Name="title" Content="Please wait while processing.." FontSize="17.333"/>
    </Grid>
    <Grid Grid.Row="1" Margin="0,0,0,20">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ProgressBar Name="progress" IsIndeterminate="True" Value="{Binding CurrentStep, Converter={StaticResource stepToProgress}}" />
    </Grid>
</Grid>

我在运行时遇到的错误是:

BindingExpression 路径错误:在“对象”“字符串”(HashCode=1137317725)上找不到“CurrentStep”属性。绑定表达式:路径=当前步骤; DataItem='String' (HashCode=1137317725);目标元素是'ProgressBar'(名称='progress');目标属性是“值”(类型“双”)

有什么帮助吗?

谢谢!

【问题讨论】:

  • 您为什么要尝试将进度条与另一​​个窗口中的属性链接?为什么不只是在显示MessageWithProgressBar 时注入一个值?如果无法将值注入 MessageWithProgressBar,则考虑将工作作为委托注入,以便可以在 MessageWithProgressBar 中捕获和测量它。
  • @slugster 在 MainWindow.xaml.cs 我处理作业,在处理作业时,我想将另一个窗口(显示进度条)作为线程提升,进度值将根据作业步骤更新如上所述在 MainWindow.xaml.cs 中设置
  • 我猜到你想做什么,但你做错了......你需要一个对话服务,那里有很多不同程度的例子。以下是帮助您入门的两个之前的 SO 问题:Good or bad practice for Dialogs in wpf with MVVM?Standard Approach of Launching Dialogs/Child Windows from a WPF Application using MVVM

标签: c# wpf data-binding namespaces


【解决方案1】:

您必须设置 MessageBox 的 DataContext。您可以在创建期间在构造函数中传递 MainWindow:

MessageWithProgressBar.xaml.cs

public MessageWithProgressBar(MainWindow Mw)
{
  this.DataContext = Mw;
}

现在您可以仅使用其名称绑定每个属性。你的进度条现在看起来像

<ProgressBar Name="progress" IsIndeterminate="True" Value="{Binding CurrentStep, Converter={StaticResource stepToProgress}}"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2011-03-03
    相关资源
    最近更新 更多