1

さまざまなオブジェクトにバインドしたい xaml ファイルにいくつかの項目があります。2 つのボタンを使用した例を示します。

<Button Content="Edit Operators" 
Command="{Binding ElementName=treeRevisions,
Path=SelectedItem.WorkOrderRev.Operators.EditCommand}" CommandParameter="" />
<Button Content="Edit MyData" 
Command="{Binding ElementName=treeRevisions,
Path=SelectedItem.WorkOrderRev.MyData.EditCommand}" CommandParameter="" />

ご覧のとおり、「Binding ElementName=treeRevisions, Path=SelectedItem.WorkOrderRev」というテキストは、両方のボタンで同じです。それは機能し、このように使用できますが、他にも多くのコントロールがあります。これを短縮して「.Operators.EditCommand」などを追加する方法はありますか? 私はどこでも解決策を探しましたが、何も見つかりませんでした。

4

2 に答える 2

0

いくつかの異なる解決策をテストし、非常に単純な解決策を見つけました。次のように、周囲のグリッドに DataContext を設定します。

    <Grid DataContext="{Binding ElementName=treeRevisions, Path=SelectedItem.WorkOrderRev.PrintData}">

次に、次のような略語を使用できます。

    <TextBox Height="23" HorizontalAlignment="Left" Margin="103,63,0,0" Name="txtDescription" VerticalAlignment="Top" Width="120" 
     IsEnabled="{Binding Path=UnLocked, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
     IsReadOnly="{Binding Path=Locked, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
     Text="{Binding Path=TableData.Rows[0][description], UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
     />

ここで、Locked、UnLocked、および TableData は SelectedItem.WorkOrderRev.PrintData のプロパティです。

于 2013-03-26T13:22:29.147 に答える
0

これを行うにはいくつかの方法があります。非常にトリッキーなものを投稿します。警告これはwpf proのクレイジーな答えですが、うまくいきます。ハハハ...

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new { D = "Hello" };
        }

        public static bool GetDoMyBinding(DependencyObject obj)
        {
            return (bool)obj.GetValue(DoMyBindingProperty);
        }

        public static void SetDoMyBinding(DependencyObject obj, bool value)
        {
            obj.SetValue(DoMyBindingProperty, value);
        }

        // Using a DependencyProperty as the backing store for DoMyBinding.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DoMyBindingProperty =
            DependencyProperty.RegisterAttached("DoMyBinding", typeof(bool),  typeof(MainWindow), new PropertyMetadata(false, new PropertyChangedCallback(OnDoMyBindingChanged)));

        private static void OnDoMyBindingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Binding binding = new Binding("D");
            binding.Mode = BindingMode.OneWay;
            if (d is Button)
            {
                Button b = d as Button;
                b.SetBinding(Button.ContentProperty, binding);
            }
            if (d is TextBlock)
            {
                TextBlock tb = d as TextBlock;
                tb.SetBinding(TextBlock.TextProperty, binding);
            }
        }

XAML は次のようになります。

    <Window x:Class="BindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:BindingTest">
    <StackPanel>
        <Button local:MainWindow.DoMyBinding="True"/>
        <Button  local:MainWindow.DoMyBinding="True"/>
        <TextBlock  local:MainWindow.DoMyBinding="True" />
    </StackPanel>
    </Window>

結果は次のようになります。

ここに画像の説明を入力

編集済み: True の代わりに、".Operators.EditCommand" などのカスタム文字列を配置できますが、ロジックを変更する必要があります。

楽しむ。

于 2013-03-08T23:39:24.450 に答える