开发者

How to create a list of paired properties that can be two-way-bound in WPF?

开发者 https://www.devze.com 2023-01-13 02:18 出处:网络
Edit 2 Ok, here is the full example: public class FixedSizeLibraryViewModel : ViewModel { RawFixedLibrary _library;

Edit 2 Ok, here is the full example:

public class FixedSizeLibraryViewModel : ViewModel
{
    RawFixedLibrary _library;

    public FixedSizeLibraryViewModel(RawFixedLibrary thisLibrary)
    {
        _library = thisLibrary;
    }

    public string BookOneTitle
    {
        get { return _library.BookOne.Title; }
        set { _library.BookThree.Title = value;
            this.OnPropertyChanged("BookOneTitle");}
    }

    public string BookTwoTitle
    {
        get { return _library.BookThree.Title; }
        set { _library.BookOne.Title = value;
        this.OnPropertyChanged("BookTwoTitle");}
    }

    public string BookThreeTitle
    {
        get { return _library.BookThree.Title; }
        set { _library.BookThree.Title = value;
        this.OnPropertyChanged("BookThreeTitle");}
    }

    public string BookOneText
    {
        get { return _library.BookOne.Text; }
        set { _library.BookThree.Text = value;
        this.OnPropertyChanged("BookOneText");}
    }

    public string BookTwoText
    {
        get { return _library.BookThree.Text; }
        set { _library.BookOne.Text = value;
        this.OnPropertyChanged("BookTwoText");}
    }

    public string BookThreeText
    {
        get { return _library.BookThree.Text; }
        set { _library.BookThree.Text = value;
        this.OnPropertyChanged("BookThreeText");}
    }

    public ????? BookList{
        get{
            if(someCondition)
                //Return Book 1 and book 2 title and text in some sort of list
            else
                //Return Book 2 and book 3 title and text in some sort of list
        }
    }
}

And binding pretty much exactly as before:

    <TabControl ItemsSource="{Binding BookList}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding BookTitle}"/>
            </DataTemplate> 
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <TextBox Text="{Binding BookContents, Mode=TwoWay}"/>
            </DataTemplate> 
        </TabControl.ContentTemplate>
    </TabControl>

In the full implementation, the book titles and texts are actually very secondary to the rest of the functions of the FixedSizeLibraryViewModel. So the work around I implemented was to simply add one tab for each book, bind the header to the book title and bind the text to the book text, then conditionally hide the tabs based on an additional public property.

This is problematic if we ever need to change the books for obvious reasons. I can bind individually to BookNTitle and BookNText just fine. Changes made to those show up in the original RawFixedLibrary that was passed in. But I can't find a way to add them to a list and have changes made to them still affect the original RawFixedLibrary.

And ViewModel implments the requirements for the OnPropertyChanged calls.

End edit 2

Edit Sorry, in reality my situation is a little more complicated than I first let on. So, in reality the class looks more like this:

    private rawBookData _thisBook;

    public Book(rawBookData rawBook)
    {
        _thisBook = rawBook
        BookTitle = _thisBook.Title;
        BookText = _thisBook.Text;
    }

    public string BookTitle
    {
        get { return _thisBook.Title; }
        set { _thisBook.Title = value; }
    }

    public string BookContent
    {
        get { return _thisBook.Text; }
        set { _thisBook.Text = value; }
    }

EndEdit

I think I have been behind the monitor too long today, I feel like this is really simple and I just cant figure it out.

I have a class with some properties, namely a book title and book contents:

public class Book
{
    private string _boookTitle;
    private string _bookContent;

    public Book(string title, string content)
    {
        _boookTitle = title;
        _bookContent = content;
    }

    public string BookTitle
    {
        get { return _boookTitle; }
        set { _boookTitle = value; }
    }

    public string BookContent
    {
        get { return _bookContent; }
        set { _bookContent = value; }
    }

    public ?????? GetStrings()
    {
        ?????? stringPair;
        //I want this to return the BookTitle and BookContent as some sort of pair
        return stringPair;
    }
}

I need this GetStrings function to pass out something that I can bind to such that I can access and set the BookTitle and BookContents via binding.

I need to ultimately make a list of these objects so I can have a tab per book in a tab control. The idea being that each tab will have the book title, then you can modify the text of the book from within that tab.

I would really like to be able to just pass a list of Books, but due to some other constraints thats not really an easy option here.

So I will have something along the lines of:

    <TabControl ItemsSource="{Binding BookList}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding BookTitle}"/>
            </DataTemplat开发者_如何学Pythone> 
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <TextBox Text="{Binding BookContents, Mode=TwoWay}"/>
            </DataTemplate> 
        </TabControl.ContentTemplate>
    </TabControl>


Why not just create a class to hold them:

public BookStuff
{
     public string BookContents {get; set;}
     public string BookTitle {get; set;}
}

Then, in your book class

public BookStuff MyBookStuff {get; set;}

If you need a collection to bind to, I would suggest an ObservableCollection:

public ObservableCollection<BookStuff> MyListOfBooks {get; set;}

then, set your ItemsSource to MyListOfBooks, and in your ItemTemplate just bind to BookTitle and BookContents

<TextBox Text="{Binding BookTitle}" ... />
0

精彩评论

暂无评论...
验证码 换一张
取 消