gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:55:57 37 4
gpt4 key购买 nike

在 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/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com