Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListBox内部绑定,需要更新_C#_Wpf_Binding_Listbox - Fatal编程技术网

C# ListBox内部绑定,需要更新

C# ListBox内部绑定,需要更新,c#,wpf,binding,listbox,C#,Wpf,Binding,Listbox,我在应用程序中使用列表框 <ListBox Name="lbMain"> <ListBox.ItemTemplate> <DataTemplate> <Expander Header="{Binding Converter={StaticResource convCaption}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" IsExpande

我在应用程序中使用列表框

<ListBox Name="lbMain">
   <ListBox.ItemTemplate>
     <DataTemplate>
        <Expander Header="{Binding Converter={StaticResource convCaption}, 
          Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" IsExpanded="True">
        <StackPanel>
          <TextBlock Text={Binding FirstName, Mode=TwoWay,
                                             UpdateSourceTrigger=PropertyChanged}>
          </TextBlock> 
           <TextBlock Text={Binding SecondName, Mode=TwoWay,   pdateSourceTrigger=PropertyChanged}>
          </TextBlock> 
        </StackPanel>
     </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>
这是一个模型

public class MyModel
{
   public string FirstName{get; set; }
   public string SecondName{get; set; }
}
这是转换器(convCaption)


一切正常。但当我更改FirstName或SecondName时,我需要在Expander中更改头。如果我写入{Mode=TwoWay}显示错误-“需要路径”。我喜欢为Expander编写正确的绑定(以标题显示更新的)?

您需要在
MyModel
类上实现
INotifyPropertyChanged
接口。请参阅MSDN上的页面以获取帮助。此外,您实际上不需要使用
转换器
来连接这两个名称。。。有一个更简单的方法。。。只需重写该类中的
ToString()
方法:

public override string ToString()
{
    return string.Format("This is {0}, {1}", FirstName, SecondName);
}

您需要在
MyModel
类上实现
INotifyPropertyChanged
接口。请参阅MSDN上的页面以获取帮助。此外,您实际上不需要使用
转换器
来连接这两个名称。。。有一个更简单的方法。。。只需重写该类中的
ToString()
方法:

public override string ToString()
{
    return string.Format("This is {0}, {1}", FirstName, SecondName);
}

首先,您的
MyModel
需要在每次
FirstName
SecondName
发生更改时实施
INotifyPropertyChanged
并引发
PropertyChanged
事件,如下所示:

public class MyModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private string _firstName;

    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (_firstName == value) return;
            _firstName = value;
            OnPropertyChanged("FirstName");
        }
    }

    private string _secondName;

    public string SecondName
    {
        get { return _secondName; }
        set
        {
            if (_secondName == value) return;
            _secondName = value;
            OnPropertyChanged("SecondName");
        }
    }
}
然后您可以将
MultiBinding
StringFormat
一起使用,如下所示:

<Expander>
    <Expander.Header>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="{}This is {0} {1}">
                    <Binding Path="FirstName"/>
                    <Binding Path="SecondName"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </Expander.Header>
</Expander>

首先,每当
FirstName
SecondName
发生更改时,您的
MyModel
需要实现
INotifyPropertyChanged
并引发
PropertyChanged
事件,如下所示:

public class MyModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private string _firstName;

    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (_firstName == value) return;
            _firstName = value;
            OnPropertyChanged("FirstName");
        }
    }

    private string _secondName;

    public string SecondName
    {
        get { return _secondName; }
        set
        {
            if (_secondName == value) return;
            _secondName = value;
            OnPropertyChanged("SecondName");
        }
    }
}
然后您可以将
MultiBinding
StringFormat
一起使用,如下所示:

<Expander>
    <Expander.Header>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="{}This is {0} {1}">
                    <Binding Path="FirstName"/>
                    <Binding Path="SecondName"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </Expander.Header>
</Expander>


您应该首先尝试正确绑定(最好是在xaml中)。。。您只需要更改属性并引发(实现)PropertyChange。您应该首先尝试正确绑定(最好是在xaml中)。。。您只需更改属性并引发(实现)属性更改。您不必重写
ToString()
,也可以只使用
StringFormat
,并将其保存在xaml中,并对名称的两部分进行
多重绑定,但这肯定比多重绑定的代码少。我知道我更喜欢用哪一种。:)谢谢,但是我不喜欢使用INotifyPropertyChanged如果您不想使用
INotifyPropertyChanged
界面,那么您可以忘记使用WPF。。。它不是真正的可选。是的,它是,你可以使用dps的几乎一切。。。虽然我不推荐。确切地说,它是如何减少代码量的呢?您不必重写
ToString()
,也可以使用
StringFormat
,并将其保存在xaml中,在名称的两个部分使用
多重绑定。我知道我更喜欢用哪一种。:)谢谢,但是我不喜欢使用INotifyPropertyChanged如果您不想使用
INotifyPropertyChanged
界面,那么您可以忘记使用WPF。。。它不是真正的可选。是的,它是,你可以使用dps的几乎一切。。。虽然我不推荐。确切地说,它是如何减少代码的?