1

私はしばらくこれに苦労してきました.WindowFormsで行うのは簡単だったでしょう.

IRC クライアントを作成しています。接続先のチャネルごとに 1 つのタブがいくつかあります。各タブには、UserList、MessageHistory、Topic など、さまざまなものを表示する必要があります。

WindowForms では、TabItem から継承し、いくつかのカスタム プロパティとコントロールを追加して完了します。

WPFでは、それを行う方法を考え出す際にいくつかの小さな問題があります。

私はそれを行う多くの方法を試しましたが、以下は私の現在の方法ですが、TextBox を Topic プロパティにバインドすることはできません。

    <Style TargetType="{x:Type t:IRCTabItem}" BasedOn="{StaticResource {x:Type TabItem}}" >
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="540" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <StackPanel Grid.Column="0">
                        <TextBox Text="{Binding Topic, RelativeSource={RelativeSource AncestorType={x:Type t:IRCTabItem}}}" />   
                    </StackPanel>
                </Grid>
            </DataTemplate>              
        </Setter.Value>
    </Setter>
</Style>

そしてコードビハインド

 public class IRCTabItem : TabItem
    {
        static IRCTabItem()
        {
            //This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
            //This style is defined in themes\generic.xaml
            //DefaultStyleKeyProperty.OverrideMetadata(typeof(IRCTabItem),
             //   new FrameworkPropertyMetadata(typeof(IRCTabItem)));
        }

        public static readonly RoutedEvent CloseTabEvent =
            EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble,
                typeof(RoutedEventHandler), typeof(IRCTabItem));

        public event RoutedEventHandler CloseTab
        {
            add { AddHandler(CloseTabEvent, value); }
            remove { RemoveHandler(CloseTabEvent, value); }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            Button closeButton = base.GetTemplateChild("PART_Close") as Button;
            if (closeButton != null)
                closeButton.Click += new System.Windows.RoutedEventHandler(closeButton_Click);
        }

        void closeButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(CloseTabEvent, this));
        }

        public bool Closeable
        {
            get { return (bool)GetValue(CloseableProperty); }
            set { SetValue(CloseableProperty, value); }
        }
        public static readonly DependencyProperty CloseableProperty = DependencyProperty.Register("Closeable", typeof(bool), typeof(IRCTabItem), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));


        public List<String> UserList
        {
            get { return (List<string>)GetValue(UserListProperty); }
            set { SetValue(UserListProperty, value); }
        }
        public static readonly DependencyProperty UserListProperty = DependencyProperty.Register("UserList", typeof(List<String>), typeof(IRCTabItem), new FrameworkPropertyMetadata(new List<String>(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public String Topic
        {
            get { return (string)GetValue(TopicProperty); }
            set { SetValue(TopicProperty, value); }
        }
        public static readonly DependencyProperty TopicProperty = DependencyProperty.Register("Topic", typeof(String), typeof(IRCTabItem), new FrameworkPropertyMetadata("Not Connected", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public bool HasAlerts
        {
            get { return (bool)GetValue(HasAlertsProperty); }
            set { SetValue(HasAlertsProperty, value); }
        }
        public static readonly DependencyProperty HasAlertsProperty = DependencyProperty.Register("HasAlerts", typeof(bool), typeof(IRCTabItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));


    }

だから私の質問は:

私はそれを正しい方法で行っていますか (ベストプラクティス)? もしそうなら、どうすれば DataTemplate をプロパティにバインドできますか? そうでない場合、私が達成しようとしていることを達成する正しい方法は何ですか?

編集 1: Peter Stephens の提案を追加 編集 2: 表示可能な編集の概要を追加 編集 3: タグ

4

2 に答える 2

0

バインディング式に相対ソースのモードがないようです。

これを試して:

<TextBox Text="{Binding Topic, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type t:IRCTabItem}}}" />

モードのデフォルトはnullであるため、これを含めないと、設定がうまく機能しない可能性があります。

于 2010-03-19T15:35:39.267 に答える
0

問題の 1 つは、依存関係プロパティが正しく実装されていないことです。

トピックは次のように実装する必要があります。

public String Topic 
{ 
    get { return (string) GetValue(TopicProperty); }
    set { SetValue(TopicProperty, value); }
}
public static readonly DependencyProperty TopicProperty = 
    DependencyProperty.Register("Topic", typeof(String), 
        typeof(IRCTabItem), new FrameworkPropertyMetadata("Not Connected", 
        FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

CLR プロパティを、WPF 依存関係プロパティを囲む構文糖衣として扱う必要があります。WPF プロパティに値の格納を処理させます。そうしないと、実質的に 2 つの異なる値を持つ 2 つのプロパティを持つことになります。

于 2010-03-19T12:14:57.133 に答える