设置ChildWindow的标题

时间:2021-04-17 07:30:05

I'm new-ish to C# and wpf. I have a ChildWindow whose title gets set from outside the class. I need the title to be shortened with ellipsis on the end if it is too long. I accomplished by doing this (the code is shortened):

我是C#和wpf的新手。我有一个ChildWindow,其标题是从课外设置的。如果标题太长,我需要在末尾用省略号缩短标题。我完成了这个(代码缩短了):

<Namespace:ChildWindow
         x:Class="Namespace.MyClass">
     <Namespace:ChildWindow.Title>
         <TextBlock x:Name="_titleBlock" Width="300" TextTrimming="WordEllipsis"/>
     </Namespace:ChildWindow.Title>
</Namespace:Childwindow>

However, I would like caller of this class to be able to set ChildWindow.Title = "Something long" rather than ChildWindow._titleBlock = "Something long" because I think that it makes more sense. Is it possible to do this through events somehow?

但是,我希望这个类的调用者能够设置ChildWindow.Title =“Something long”而不是ChildWindow._titleBlock =“Something long”因为我觉得它更有意义。有可能以某种方式通过事件来做到这一点吗?

1 个解决方案

#1


0  

What you really want to do is use the MVVM pattern to split your logic out from your view. That way you can pass your ViewModel to the thing manipulating the child window instead of the child window itself.

您真正想要做的是使用MVVM模式将您的逻辑从视图中分离出来。这样,您可以将ViewModel传递给操纵子窗口而不是子窗口本身的东西。

For example, a basic ViewModel for the child window could be:

例如,子窗口的基本ViewModel可以是:

public class ChildWindowViewModel: INotifyPropertyChanged {
    private string _title;

    public string Title{
        get { return _title; }
        set{if (value != _title){
            _title = value;
            OnPropertyChanged("Title");
        }}
    }

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

    public event PropertyChangedEventHandler PropertyChanged;
}

You then set the DataContext to the viewmodel when you create the child window like this:

然后,在创建子窗口时将DataContext设置为viewmodel,如下所示:

//Creating the child window
ChildWindow child = new ChildWindow();
ChildWindowViewModel childViewModel = new ChildWindowViewModel();
child.DataContext = childViewModel;
//do stuff with child...

and hook the child windows Title up to the ViewModel in the Xaml like this:

并将子窗口标题挂钩到Xaml中的ViewModel,如下所示:

<Namespace:ChildWindow
         x:Class="Namespace.MyClass">
     <Namespace:ChildWindow.Title>
         <TextBlock Width="300" TextTrimming="WordEllipsis" Text="{Binding Path=Title}/>
     </Namespace:ChildWindow.Title>
</Namespace:Childwindow>

Then when you want to change the title you can use

然后,当您想要更改标题时,您可以使用

childViewModel.Title = "A Very Long Title That Will Be Cut Short In Its Prime";

Setting the title on the ViewModel will trigger the PropertyChanged event which will cause the Xaml view to update itself with the newly set value. This might seem like a VERY long winded way of doing things but if you think about what this allows you to do for a few minutes you'll see some huge benefits. The binding goes way beyond simple title texts...

在ViewModel上设置标题将触发PropertyChanged事件,该事件将使Xaml视图使用新设置的值更新自身。这似乎是一种非常冗长的做事方式,但如果你想一想这会让你做几分钟,你会看到一些巨大的好处。绑定远远超出了简单的标题文本......

Hopefully that will all work as is but I'm doing it from memory so sorry for any mistakes...

希望这一切都能正常工作,但我是从记忆中做到的,所以对任何错误都感到抱歉......

#1


0  

What you really want to do is use the MVVM pattern to split your logic out from your view. That way you can pass your ViewModel to the thing manipulating the child window instead of the child window itself.

您真正想要做的是使用MVVM模式将您的逻辑从视图中分离出来。这样,您可以将ViewModel传递给操纵子窗口而不是子窗口本身的东西。

For example, a basic ViewModel for the child window could be:

例如,子窗口的基本ViewModel可以是:

public class ChildWindowViewModel: INotifyPropertyChanged {
    private string _title;

    public string Title{
        get { return _title; }
        set{if (value != _title){
            _title = value;
            OnPropertyChanged("Title");
        }}
    }

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

    public event PropertyChangedEventHandler PropertyChanged;
}

You then set the DataContext to the viewmodel when you create the child window like this:

然后,在创建子窗口时将DataContext设置为viewmodel,如下所示:

//Creating the child window
ChildWindow child = new ChildWindow();
ChildWindowViewModel childViewModel = new ChildWindowViewModel();
child.DataContext = childViewModel;
//do stuff with child...

and hook the child windows Title up to the ViewModel in the Xaml like this:

并将子窗口标题挂钩到Xaml中的ViewModel,如下所示:

<Namespace:ChildWindow
         x:Class="Namespace.MyClass">
     <Namespace:ChildWindow.Title>
         <TextBlock Width="300" TextTrimming="WordEllipsis" Text="{Binding Path=Title}/>
     </Namespace:ChildWindow.Title>
</Namespace:Childwindow>

Then when you want to change the title you can use

然后,当您想要更改标题时,您可以使用

childViewModel.Title = "A Very Long Title That Will Be Cut Short In Its Prime";

Setting the title on the ViewModel will trigger the PropertyChanged event which will cause the Xaml view to update itself with the newly set value. This might seem like a VERY long winded way of doing things but if you think about what this allows you to do for a few minutes you'll see some huge benefits. The binding goes way beyond simple title texts...

在ViewModel上设置标题将触发PropertyChanged事件,该事件将使Xaml视图使用新设置的值更新自身。这似乎是一种非常冗长的做事方式,但如果你想一想这会让你做几分钟,你会看到一些巨大的好处。绑定远远超出了简单的标题文本......

Hopefully that will all work as is but I'm doing it from memory so sorry for any mistakes...

希望这一切都能正常工作,但我是从记忆中做到的,所以对任何错误都感到抱歉......