c# - 使用转换器 WPF 绑定(bind)来自另一个 cs 文件的属性

标签 c# wpf data-binding namespaces

在 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 path error: 'CurrentStep' property not found on 'object' ''String' (HashCode=1137317725)'. BindingExpression:Path=CurrentStep; DataItem='String' (HashCode=1137317725); target element is 'ProgressBar' (Name='progress'); target property is 'Value' (type 'Double')



任何帮助?

谢谢!

最佳答案

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

MessageWithProgressBar.xaml.cs

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

现在您可以只使用其名称绑定(bind)每个属性。你的进度条现在看起来像
<ProgressBar Name="progress" IsIndeterminate="True" Value="{Binding CurrentStep, Converter={StaticResource stepToProgress}}"/>

关于c# - 使用转换器 WPF 绑定(bind)来自另一个 cs 文件的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37641587/

相关文章:

c# - 将 ajax 数据发布到 ASP.NET MVC Controller

c# - 创建自定义 DataGrid 的 ItemsSource

wpf - 标题未显示在 WPF ListView 中

c# - 如何在 WPF View XAML 中显示 List<KeyValuePair<string,string>>

c# - 是否有更简洁的方法将属性绑定(bind)到所有者的 DataContext?

wpf - 当我离开它时,为什么我的 DataGridComboBoxColumn 会清除它的值?

c# - 将多个控件数据绑定(bind)到单个 LINQ 查询

c# - Async/Await VS Task.Run : When to use ? 如何使用?

c# - 获取 .NET 方法返回值的属性数据

c# - 如何解决RDLC中的 "Report item not linked to Dataset"错误?