【问题标题】:Binding a textbox from a secondary window is not updating the property从辅助窗口绑定文本框不会更新属性
【发布时间】:2012-10-10 15:47:11
【问题描述】:

我需要将位于辅助窗口中的文本框的文本属性绑定到在该辅助窗口的相应视图模型中定义的属性

XAML 代码:

<Window x:Class="RG.IOManager.Views.PreferencesDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:genericClasses="clr-namespace:RG.IOManager.GenericClasses"
    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
    Title="Setup" Height="131" Width="332"
    WindowStartupLocation="CenterOwner" 
    ShowInTaskbar="False"
    WindowStyle="ToolWindow"
    >

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../ResourceDictionary.xaml" />
            <ResourceDictionary Source="../ScrollBarTemplate.xaml" />
            <ResourceDictionary Source="../Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="10*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Label Content="Cycle Time"  Grid.Row="0" Grid.Column="0" Height="28" HorizontalAlignment="Left" Margin="10,10,0,20" Name="label1" VerticalAlignment="Top" />
    <StackPanel Grid.Row="0" Grid.Column="1" Width="190" Orientation="Horizontal">
        <!--
        <xctk:IntegerUpDown  Name="TbMainCycleTime" Margin="10,10,0,20" Width="50" Height="25" HorizontalContentAlignment="Right" 
                             Increment="1" Maximum="5000" Minimum="0" ShowButtonSpinner="False"/>
        -->

        <TextBox Name="TbMainCycleTime" Margin="10,10,0,20" Width="50" Height="25" HorizontalContentAlignment="Right" Validation.ErrorTemplate="{StaticResource ErrorTemplate}" Style="{StaticResource textBoxErrorTooltip}" >
            <TextBox.Text>
                <Binding Source="PreferencesDialog" Path="CycleTime" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
                    <Binding.ValidationRules>
                        <genericClasses:IntegersValidation Min="0" Max="1000" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

        <!--<TextBox Name="TbMainCycleTime" Margin="10,10,0,20" Width="50" Height="25" HorizontalContentAlignment="Right"  />-->
        <Label Content="ms" Margin="1,10,10,20"/>
    </StackPanel>
    <StackPanel Grid.Row="1" Grid.Column="1" Width="190" Orientation="Horizontal">
        <Button Content="Update" Height="23" HorizontalAlignment="Left" Margin="5,0,10,0" Name="btUpdate" VerticalAlignment="Top" Width="75" Click="btUpdate_Click" />
        <Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="5,0,10,0" Name="btCancel" VerticalAlignment="Top" Width="75" Click="btCancel_Click" />
    </StackPanel>
</Grid>

CS 代码:

/// <summary>
/// Interaction logic for PreferencesDialogue.xaml
/// </summary>
public partial class PreferencesDialog : Window, INotifyPropertyChanged
{
    #region Binding Properties

    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Main cycle time
    /// </summary>
    public int CycleTime
    {
        get { return _CycleTime; }
        set
        {
            _CycleTime = value;
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs("CycleTime"));
            }
        }
    }
    private int _CycleTime;

    #endregion


    private IOManager _receiver;

    public PreferencesDialog(IOManager receiver)
    {
        this._receiver = receiver;
        InitializeComponent();

        //this.TbMainCycleTime.Text = _receiver.globalBindingProperties.MainCycleTime.ToString();
        this.CycleTime = _receiver.globalBindingProperties.MainCycleTime;
    }


    private void btUpdate_Click(object sender, RoutedEventArgs e)
    {
        _receiver.globalBindingProperties.MainCycleTime = Convert.ToInt32(this.TbMainCycleTime.Text);
        this.Close();
    }

    private void btCancel_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}

谁能帮我找出我做错了什么?提前致谢

【问题讨论】:

  • 你能告诉我们代码中的位置,DataContext 分配在哪里吗?
  • 窗口标签 定义了属性“CycleTime”所在的类不?跨度>

标签: wpf binding window


【解决方案1】:

你的方法是错误的。但是,如果您更改这两件事,您的问题将得到解决:

先设置Window的DataContext:

    public PreferencesDialog(IOManager receiver)
{
    this.DataContext = this;

    this._receiver = receiver;
    InitializeComponent();

    //this.TbMainCycleTime.Text = _receiver.globalBindingProperties.MainCycleTime.ToString();
    this.CycleTime = _receiver.globalBindingProperties.MainCycleTime;
}

第二次从 TextBox.Text.Binding 中删除“源”,因为源是 dataContext。

    <TextBox Name="TbMainCycleTime" Margin="10,10,0,20" Width="50" Height="25" HorizontalContentAlignment="Right" Validation.ErrorTemplate="{StaticResource ErrorTemplate}" Style="{StaticResource textBoxErrorTooltip}" >
        <TextBox.Text>
            <Binding Path="CycleTime" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
                <Binding.ValidationRules>
                    <genericClasses:IntegersValidation Min="0" Max="1000" />
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

【讨论】:

    【解决方案2】:

    您是否已将视图模型作为数据上下文分配给窗口? (您在其中定义了 CycleTime 的类)

    其次,您是否要更改 UI 的周期时间。您在 TextBox 和 CycleTime 属性之间使用两种方式绑定。

    检查这个link,我在其中创建了类型安全文本框扩展。虽然,文章是关于 silverlight,但您可以在 WPF 中轻松使用。

    将您的 xaml 更改为

    <Window x:Class="RG.IOManager.Views.PreferencesDialog" x:Name="PreferencesDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    

    【讨论】:

    • 如何将“PreferencesDialog”类作为数据上下文分配给窗口。我以为我已经这样做了,因为窗口标签包含类“PreferencesDialog”->
    • 我有点担心你写的那种代码。请尝试谷歌搜索“WPF MVVM”并查看如何使用视图模型作为数据上下文来查看(窗口/控件)
    • 我尝试更改 xaml,但编译时出现此错误:“成员名称不能等于相应的周围成员”
    猜你喜欢
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 2017-03-18
    相关资源
    最近更新 更多